英文:
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("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?
答案1
得分: 2
那个包中的命名空间功能似乎有问题。请参考以下链接:
答案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("/room1").On("connection", 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 (
"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
答案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('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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论