Node.js上的WebSocket,并在所有连接的客户端之间共享消息
WebSocket on Node.js and share a message between all the connected clients
我有一个带有websocket
模块的Node.js服务器,通过以下命令安装:
npm install websocket
从本指南开始,我决定扩展它,在所有客户端之间共享发送的消息。
这是我的(简化的)服务器代码:
#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
var connectedClientsCount = 0; // ADDED
var connectedClients = []; // ADDED
wsServer.on('request', function(request) {
var connection = request.accept('echo-protocol', request.origin);
connectedClientsCount++;
connectedClients.push(connection);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
for(c in connectedClients) // ADDED
c.sendUTF(message.utf8Data); // ADDED
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
// here I should delete the client...
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
在这种情况下,我可以获得connectedClientsCount
值,但不能管理connectedClients
列表。
我也尝试过使用((eval)c).sendUTF(message.utf8Data);
作为语句,但它不起作用。
我建议您使用Socket。IO:用于实时应用程序的跨浏览器WebSocket。该模块安装和配置非常简单
例如:服务器
...
io.sockets.on('connection', function (socket) {
//Sends the message or event to every connected user in the current namespace, except to your self.
socket.broadcast.emit('Hi, a new user connected');
//Sends the message or event to every connected user in the current namespace
io.sockets.emit('Hi all');
//Sends the message to one user
socket.emit('news', {data:'data'});
});
});
...
更多
客户:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
//receive message
socket.on('news', function (data) {
console.log(data);
//send message
socket.emit('my other event', { my: 'data' });
});
</script>
有关暴露事件的更多信息
尝试用for ... of
替换for ... in
for(c of connectedClients) // ADDED
c.sendUTF(message.utf8Data); // ADDED
相关文章:
- Node.js上的WebSocket,并在所有连接的客户端之间共享消息
- SignalR客户端启动连接时如何设置用户
- 服务器上的Meteor客户端断开连接事件
- 可以't连接客户端和服务器import.io
- 当客户端在JW Player中恢复丢失的连接时,会自动重新连接
- websocket客户端在发送大数据时断开连接
- socket.io:断开连接事件 - “传输关闭”、“客户端命名空间断开连接”、“传输错误”和“强制关闭”
- 单个 Redis 客户端连接是否支持并发读写
- 多个 socket.io 客户端连接不起作用(谷歌浏览器,火狐浏览器)
- Socket.IO socket.id 更改同一客户端而不重新连接
- Node和Socket.io…阻止外部客户端连接
- 如何连接Node.js和Android作为客户端
- 在客户端断开连接时释放事件处理程序 Socket.IO
- 为什么可以't我得到连接客户端's的ip地址在我的服务器上运行Node.js+Openshift托管的W
- 测试客户端'通过https-ASP.net连接的能力
- 如何在自动重新连接超时后重新连接socket.io客户端
- 连接客户端服务器在ASP.net MVC信号R
- 套接字.已连接客户端的IO刷新列表
- 套接字IO:检索特定连接客户端套接字的最佳方法
- 使用HTTP连接客户端到Node.js服务器