简体   繁体   中英

Broadcasting with shoe sockjs

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM