最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io not emittingreceiving events while using namespaces - Stack Overflow

programmeradmin3浏览0评论

I have the following code written in coffeescript that makes use of socket.io and node.js in the server side

Server

io.of("/room").authorization (handshakeData, callback) ->
    #Check if authorized
    callback(null,true)
.on 'connection',  (socket) ->
    console.log "connected!"

    socket.emit 'newMessage', {msg: "Hello!!", type: 1} 

    socket.on 'sendMessage', (data) ->
        @io.sockets.in("/room").emit 'newMessage', {msg: "New Message!!", type: 0} 

Client

socket = io.connect '/'

socket.of("/room")
    .on 'connect_failed',  (reason) ->
    console.log 'unable to connect to namespace', reason
.on 'connect', ->
    console.log 'sucessfully established a connection with the namespace'

socket.on 'newMessage', (message) ->
    console.log "Message received: #{message.msg}"

My problem is that after I started using namespaces the munication between server and client has stopped working. I didn't find any working example similar to this so I might be doing something wrong

I have the following code written in coffeescript that makes use of socket.io and node.js in the server side

Server

io.of("/room").authorization (handshakeData, callback) ->
    #Check if authorized
    callback(null,true)
.on 'connection',  (socket) ->
    console.log "connected!"

    socket.emit 'newMessage', {msg: "Hello!!", type: 1} 

    socket.on 'sendMessage', (data) ->
        @io.sockets.in("/room").emit 'newMessage', {msg: "New Message!!", type: 0} 

Client

socket = io.connect '/'

socket.of("/room")
    .on 'connect_failed',  (reason) ->
    console.log 'unable to connect to namespace', reason
.on 'connect', ->
    console.log 'sucessfully established a connection with the namespace'

socket.on 'newMessage', (message) ->
    console.log "Message received: #{message.msg}"

My problem is that after I started using namespaces the munication between server and client has stopped working. I didn't find any working example similar to this so I might be doing something wrong

Share Improve this question edited Sep 22, 2013 at 14:45 Gorka Lauzirika asked Sep 22, 2013 at 13:00 Gorka LauzirikaGorka Lauzirika 3135 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Namespaces aren't used on the client like they are used server side. Your client side code should connect directly to the namespace path like this:

var socket = io.connect('/namespace');
socket.on('event', function(data) {
  // handle event
});

That being said, namespaces are different than rooms. Namespaces are joined on the client side, while rooms are joined on the server side. Therefore this code won't work:

io.sockets.in('/namespace').emit('event', data);

You have to either reference the namespace, or call it from the global io object.

var nsp = io.of('/namespace');
nsp.emit('event', data);

// or get the global reference
io.of('/namespace').emit('event', data);
发布评论

评论列表(0)

  1. 暂无评论