dongzhan1383 2015-01-16 11:20
浏览 134

无法使名称空间在go-socketio中工作

I'm using https://github.com/googollee/go-socket.io to create a socket.io server. I'm trying to create a namespace, but I'm not able to connect to the namespace from the client side.

Server:

func registerHandlers(server *socketio.Server) {
    server.Of("room1").On("connection", connectionHandler)
}

func connectionHandler(so socketio.Socket) {
    log.Println("on connection")
    so.Join("chat")
    so.On("chat message", func(msg string) {
        so.BroadcastTo("chat", "chat message", msg)
    })
}

Client:

var socket = io.connect("http://localhost:3000/room1");
socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });

Am I missing something?

  • 写回答

4条回答 默认 最新

  • doumi7854 2015-01-17 20:53
    关注

    On Server side

    By default socket.io works in the default namespace (/)

    var io = require('socket.io')(http);
    

    You than create namespaces from the default.

    var nmspc = io.of('/namespace');
    

    Now you can emit and receive messages within your custum namespace.

    nmspc.on('connection', function(socket){
         socket.on('', function(){ 
            ...
         });
    
    }
    

    On the client side you can connect to the namespace with the following

    var socket = io('/namespace').connect('http://url:PORT');
    

    I hope this covers your question around namespaces within socket.io

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集