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

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

Cannot make namespaces work in go-socketio

问题

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

服务器端代码:

  1. func registerHandlers(server *socketio.Server) {
  2. server.Of("room1").On("connection", connectionHandler)
  3. }
  4. func connectionHandler(so socketio.Socket) {
  5. log.Println("on connection")
  6. so.Join("chat")
  7. so.On("chat message", func(msg string) {
  8. so.BroadcastTo("chat", "chat message", msg)
  9. })
  10. }

客户端代码:

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

我有什么遗漏的吗?

英文:

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:

  1. func registerHandlers(server *socketio.Server) {
  2. server.Of(&quot;room1&quot;).On(&quot;connection&quot;, connectionHandler)
  3. }
  4. func connectionHandler(so socketio.Socket) {
  5. log.Println(&quot;on connection&quot;)
  6. so.Join(&quot;chat&quot;)
  7. so.On(&quot;chat message&quot;, func(msg string) {
  8. so.BroadcastTo(&quot;chat&quot;, &quot;chat message&quot;, msg)
  9. })
  10. }

Client:

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

Am I missing something?

答案1

得分: 2

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

英文:

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

答案2

得分: 0

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

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

试试看吧。

英文:

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

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

Give that a try.

答案3

得分: 0

使用此示例:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/googollee/go-socket.io"
  6. )
  7. func main() {
  8. server, err := socketio.NewServer(nil)
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. // server.SetMaxConnection(9000000);
  13. server.On("connection", func(so socketio.Socket) {
  14. log.Println("on connection")
  15. log.Println(so.Id())
  16. so.On("CHAT", func(msg string) {
  17. log.Println("CHAT +++++++>", msg)
  18. })
  19. so.On("server", func(msg string) {
  20. log.Println("serverpublic =====> ", msg)
  21. })
  22. so.On("disconnection", func() {
  23. log.Println("on disconnect")
  24. })
  25. })
  26. server.On("error", func(so socketio.Socket, err error) {
  27. log.Println("error:", err)
  28. })
  29. http.Handle("/socket.io/", server)
  30. log.Println("Serving at localhost:16900...")
  31. log.Fatal(http.ListenAndServe(":15900", nil))
  32. }//main
英文:

use this sample :

  1. package main
  2. import (
  3. &quot;log&quot;
  4. &quot;net/http&quot;
  5. &quot;github.com/googollee/go-socket.io&quot;
  6. )
  7. func main() {
  8. server, err := socketio.NewServer(nil)
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. // server.SetMaxConnection(9000000);
  13. server.On(&quot;connection&quot;, func(so socketio.Socket) {
  14. log.Println(&quot;on connection&quot;)
  15. log.Println(so.Id())
  16. so.On(&quot;CHAT&quot;, func(msg string) {
  17. log.Println(&quot;CHAT +++++++&gt;&quot; , msg)
  18. })
  19. so.On(&quot;server&quot;, func(msg string) {
  20. log.Println(&quot;serverpublic =====&gt; &quot; , msg)
  21. })
  22. so.On(&quot;disconnection&quot;, func() {
  23. log.Println(&quot;on disconnect&quot;)
  24. })
  25. })
  26. server.On(&quot;error&quot;, func(so socketio.Socket, err error) {
  27. log.Println(&quot;error:&quot;, err)
  28. })
  29. http.Handle(&quot;/socket.io/&quot;, server)
  30. log.Println(&quot;Serving at localhost:16900...&quot;)
  31. log.Fatal(http.ListenAndServe(&quot;:15900&quot;, nil))
  32. }//main

答案4

得分: -5

在服务器端

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

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

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

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

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

  1. nmspc.on('connection', function(socket){
  2. socket.on('', function(){
  3. ...
  4. });
  5. });

在客户端

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

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

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

英文:

On Server side

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

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

You than create namespaces from the default.

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

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

  1. nmspc.on(&#39;connection&#39;, function(socket){
  2. socket.on(&#39;&#39;, function(){
  3. ...
  4. });
  5. }

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

  1. 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:

确定