AFAIK,正如你所说,你必须用“手”滚动它。这是我所做的:
server.js:
var shoe = require('shoe')
var connectedClients = {}
var conCount = 0
var sock = shoe(function(clientStream) {
clienStream.id = conCount
connectedClients[clientStream.id] = clientStream
conCount += 1
})
服务器端程序中的其他地方:
//write to all connected clients
Object.keys(connectedClients).forEach(function(cid) {
var clientStream = connectedClients[cid]
clientStream.write(yourData)
})
请注意,您需要引入额外的逻辑以仅写入连接的客户端,因此您需要从 中删除断开连接的客户端connectedClients
,例如delete connectedClients[id]
.
希望这会有所帮助。