无法在go-socketio中使用命名空间。

huangapple go评论94阅读模式
英文:

Cannot make namespaces work in go-socketio

问题

我正在使用https://github.com/googollee/go-socket.io创建一个socket.io服务器。我正在尝试创建一个命名空间,但是我无法从客户端连接到该命名空间。

服务器端代码:

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

客户端代码:

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

我有什么遗漏的吗?

英文:

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(&quot;room1&quot;).On(&quot;connection&quot;, connectionHandler)
}

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

Client:

var socket = io.connect(&quot;http://localhost:3000/room1&quot;);
socket.on(&#39;chat message&#39;, function(msg){
        $(&#39;#messages&#39;).append($(&#39;&lt;li&gt;&#39;).text(msg));
      });

Am I missing something?

答案1

得分: 2

那个包中的命名空间功能似乎有问题。请参考以下链接:

英文:

The namespace functionality in that package seems to be broken. See:

答案2

得分: 0

你想在命名空间前加上斜杠。
你给出的示例代码将变为:

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

试试看吧。

英文:

You want to prefix your namespaces with a slash.
The code you gave as an example would become:

func registerHandlers(server *socketio.Server) {
    server.Of(&quot;/room1&quot;).On(&quot;connection&quot;, connectionHandler)
}

Give that a try.

答案3

得分: 0

使用此示例:

package main

import (
    "log"
    "net/http"
    "github.com/googollee/go-socket.io"
)


func main() {


    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    // server.SetMaxConnection(9000000);

    server.On("connection", func(so socketio.Socket) {
        log.Println("on connection")
        log.Println(so.Id())



        so.On("CHAT", func(msg string) {
            log.Println("CHAT +++++++>", msg)
        })


        so.On("server", func(msg string) {
            log.Println("serverpublic  =====>  ", msg)
        })



        so.On("disconnection", func() {
            log.Println("on disconnect")
        })

    })


    server.On("error", func(so socketio.Socket, err error) {
        log.Println("error:", err)
    })


    http.Handle("/socket.io/", server)
    log.Println("Serving at localhost:16900...")
    log.Fatal(http.ListenAndServe(":15900", nil))
}//main
英文:

use this sample :

package main

import (
    &quot;log&quot;
    &quot;net/http&quot;
    &quot;github.com/googollee/go-socket.io&quot;
)


func main() {


    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    // server.SetMaxConnection(9000000);

    server.On(&quot;connection&quot;, func(so socketio.Socket) {
        log.Println(&quot;on connection&quot;)
        log.Println(so.Id())



        so.On(&quot;CHAT&quot;, func(msg string) {
            log.Println(&quot;CHAT +++++++&gt;&quot;   , msg)
        })


        so.On(&quot;server&quot;, func(msg string) {
            log.Println(&quot;serverpublic  =====&gt;  &quot;   , msg)
        })



        so.On(&quot;disconnection&quot;, func() {
            log.Println(&quot;on disconnect&quot;)
        })

    })


    server.On(&quot;error&quot;, func(so socketio.Socket, err error) {
        log.Println(&quot;error:&quot;, err)
    })


    http.Handle(&quot;/socket.io/&quot;, server)
    log.Println(&quot;Serving at localhost:16900...&quot;)
    log.Fatal(http.ListenAndServe(&quot;:15900&quot;, nil))
}//main

答案4

得分: -5

在服务器端

默认情况下,socket.io在默认命名空间(/)中工作。

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

然后,您可以从默认命名空间创建命名空间。

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

现在,您可以在自定义命名空间内发送和接收消息。

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

在客户端

您可以使用以下代码连接到命名空间:

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

希望这样回答了您关于socket.io中命名空间的问题。

英文:

On Server side

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

var io = require(&#39;socket.io&#39;)(http);

You than create namespaces from the default.

var nmspc = io.of(&#39;/namespace&#39;);

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

nmspc.on(&#39;connection&#39;, function(socket){
     socket.on(&#39;&#39;, function(){ 
        ...
     });

}

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

var socket = io(&#39;/namespace&#39;).connect(&#39;http://url:PORT&#39;);

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

huangapple
  • 本文由 发表于 2015年1月16日 19:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/27982862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定