go-socket.io
go-socket.io copied to clipboard
How do I find an instance of socketio.Conn corresponding to s.id ()
I am trying to send a message to the specified s.ID ()
,but I cannot find the corresponding socketio.Conn
via s.Id ().
A bit more of an example code would help to solve your problem. Please don't share sensitive content.
@p000ic
A bit more of an example code would help to solve your problem. Please don't share sensitive content.
I looked at the example in the readme.md
, where the conn.Emit()
is used in the callback function that the server receives when the client sends data.So you've got the current conn,
However, in my current scenario, the server needs to receive the SID from another system interface and find the corresponding conn according to the SID.Then send the message via conn.Emit()]
system
@p000ic
A bit more of an example code would help to solve your problem. Please don't share sensitive content.
I looked at the example in the
readme.md
, where theconn.Emit()
is used in the callback function that the server receives when the client sends data.So you've got the current conn, However, in my current scenario, the server needs to receive the SID from another system interface and find the corresponding conn according to the SID.Then send the message via conn.Emit()]
U can store the ID->conn mapping data by yourself when the conn connecting,it's easy way
var SocketConMap = make(map[string]socketio.Conn)
server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
userID := getCookieByName("userID", getCookies(s.RemoteHeader().Get("Cookie")))
fmt.Println("connected:", s.ID(), userID)
SocketConMap[userID] = s
return nil
})
if v, ok := util.SocketConMap[uid]; ok {
time := time.Now().Format("15:04:05.000")
v.Emit("msg", util.SocketData{Time: time, Msg: "服务端发送消息到指定客户端"})
}