【发布时间】:2018-09-07 13:32:06
【问题描述】:
我在节点中有一个套接字服务器。当它恢复一条新消息时,它会将其写入套接字。但它已从中恢复,写入相应的套接字,而不是所有连接。
服务器.js
var server = net.createServer(function(sock){
console.log('new client connected');
sock.on('data', function(data) {
console.log('Server received');
// ** NOT sending to all clients **
sock.write('broadcasting to others...');
});
});
客户端.js
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('Client connected to: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('Client is connected!!');
});
client.on('data', function(data) {
console.log('Client received: ' + data);
});
如何向所有其他客户端广播一条客户端消息?
【问题讨论】: