开发者

HTML WebSocket Server, For Communication between Different Browsers

I have been trying to implement HTML5 socket server to broadcast whatever it receives to all its connected clients but have no success.

I am new to sockets, can someone pelase suggest me if there is anything already available opensource or what really is it one has to check for doing so. All i could see is client to server communication but there is no way i can send data 开发者_开发问答from one client to server to the other client or simply put, the server just broadcast all messages to all its connected client??


It sounds like you're trying to achieve peer-to-peer communication, which isn't possible over websockets.

It wouldn't be very difficult to set up a fast broadcast server using Node.js and CoffeeScript, that just echoes everything it receives from one socket to all of the others connected:

net = require 'net'

Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1

class Client
  constructor: (@socket) ->

clients = []

server = net.createServer (socket) ->
  client = new Client(socket)
  clients.push client

  socket.addListener 'connect', ->
    socket.write "Welcome\r\n"

  socket.addListener 'data', (data) ->
    for c in clients when c isnt client
      c.socket.write data

  socket.addListener 'end', ->
    clients.remove client
    socket.end
.listen 4000

console.log "Chat server is running at localhost:4000"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜