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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745323709a4622561.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信