1. 案例一:实时聊天应用
在实时聊天应用中,WebSocket技术可以实现用户之间的即时通讯。以下是一个简单的示例:
// 客户端代码
var ws = new WebSocket('ws://服务器地址');
ws.onopen = function(event) {
ws.send('Hello, Server!');
};
ws.onmessage = function(event) {
console.log('Message from server: ' + event.data);
};
ws.onerror = function(error) {
console.log('Error: ' + error);
};
ws.onclose = function(event) {
console.log('Connection closed.');
};
// 服务器端代码
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('Hello, Client!');
});
2. 案例二:股票行情实时推送
股票行情实时推送是一种常见的应用场景,通过WebSocket技术,可以将最新的股票信息实时推送给用户。
// 客户端代码
var ws = new WebSocket('ws://服务器地址');
ws.onopen = function(event) {
ws.send('subscribe stock: AAPL');
};
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
if (data.type === 'stock') {
console.log('Stock price: ' + data.price);
}
};
ws.onerror = function(error) {
console.log('Error: ' + error);
};
ws.onclose = function(event) {
console.log('Connection closed.');
};
// 服务器端代码
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
var data = JSON.parse(message);
if (data.type === 'subscribe') {
// 处理订阅逻辑
}
});
// 模拟实时数据
setInterval(() => {
var data = {
type: 'stock',
price: Math.random() * 100
};
ws.send(JSON.stringify(data));
}, 1000);
});
3. 案例三:在线游戏
在线游戏是一种对实时性要求很高的应用场景,WebSocket技术可以实现玩家之间的实时交互。
// 客户端代码
var ws = new WebSocket('ws://服务器地址');
ws.onopen = function(event) {
ws.send('join game');
};
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
if (data.type === 'move') {
// 处理游戏移动逻辑
}
};
ws.onerror = function(error) {
console.log('Error: ' + error);
};
ws.onclose = function(event) {
console.log('Connection closed.');
};
// 服务器端代码
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
var data = JSON.parse(message);
if (data.type === 'join') {
// 处理加入游戏逻辑
}
});
// 模拟游戏数据
setInterval(() => {
var data = {
type: 'move',
position: {
x: Math.random() * 100,
y: Math.random() * 100
}
};
ws.send(JSON.stringify(data));
}, 1000);
});
4. 案例四:物联网设备监控
在物联网设备监控场景中,WebSocket技术可以实时推送设备状态信息。
// 客户端代码
var ws = new WebSocket('ws://服务器地址');
ws.onopen = function(event) {
ws.send('monitor device: 12345');
};
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
if (data.type === 'status') {
console.log('Device status: ' + data.status);
}
};
ws.onerror = function(error) {
console.log('Error: ' + error);
};
ws.onclose = function(event) {
console.log('Connection closed.');
};
// 服务器端代码
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
var data = JSON.parse(message);
if (data.type === 'monitor') {
// 处理监控逻辑
}
});
// 模拟设备状态数据
setInterval(() => {
var data = {
type: 'status',
status: Math.random() > 0.5 ? 'online' : 'offline'
};
ws.send(JSON.stringify(data));
}, 1000);
});
5. 案例五:实时位置跟踪
实时位置跟踪应用可以使用WebSocket技术将用户的位置信息实时推送给其他用户或相关应用。
// 客户端代码
var ws = new WebSocket('ws://服务器地址');
ws.onopen = function(event) {
ws.send({
type: 'track',
latitude: 34.0522,
longitude: -118.2437
});
};
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
if (data.type === 'location') {
// 处理位置信息逻辑
}
};
ws.onerror = function(error) {
console.log('Error: ' + error);
};
ws.onclose = function(event) {
console.log('Connection closed.');
};
// 服务器端代码
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
var data = JSON.parse(message);
if (data.type === 'track') {
// 处理跟踪逻辑
}
});
// 模拟位置数据
setInterval(() => {
var data = {
type: 'location',
latitude: 34.0522 + Math.random() * 0.5,
longitude: -118.2437 + Math.random() * 0.5
};
ws.send(JSON.stringify(data));
}, 1000);
});
6. 案例六:实时新闻推送
实时新闻推送应用可以将最新的新闻动态实时推送给用户。
// 客户端代码
var ws = new WebSocket('ws://服务器地址');
ws.onopen = function(event) {
ws.send('subscribe news');
};
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
if (data.type === 'news') {
console.log('News: ' + data.title);
}
};
ws.onerror = function(error) {
console.log('Error: ' + error);
};
ws.onclose = function(event) {
console.log('Connection closed.');
};
// 服务器端代码
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
var data = JSON.parse(message);
if (data.type === 'subscribe') {
// 处理订阅逻辑
}
});
// 模拟新闻数据
setInterval(() => {
var data = {
type: 'news',
title: 'News title ' + Math.random()
};
ws.send(JSON.stringify(data));
}, 1000);
});
以上六个案例展示了HTML5 WebSocket技术在实时数据交互领域的应用。通过WebSocket技术,可以实现实时聊天、股票行情实时推送、在线游戏、物联网设备监控、实时位置跟踪和实时新闻推送等多种功能。在实际开发过程中,可以根据具体需求选择合适的案例进行参考和借鉴。
