2

I am trying to setup a simple scenerio using shoe + dnode +sockjs and I do not know how to broadcast a message to all users connected to the web application.

Do you know if there is a function or method which manage this? or should it be make by "hand"?

4

1 回答 1

1

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].

希望这会有所帮助。

于 2013-09-19T13:14:31.047 回答