سؤال

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"?

هل كانت مفيدة؟

المحلول

AFAIK, you have to roll it by "hand" as you say. Here is what I do:

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
})

somewhere else in your server-side program:

//write to all connected clients
Object.keys(connectedClients).forEach(function(cid) {
  var clientStream = connectedClients[cid]
  clientStream.write(yourData)
})

Note, you'll want to introduce additional logic to only write to connected clients, so you'll want to remove disconnected clients from connectedClients, something like delete connectedClients[id].

Hopefully that helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top