我在 GoLang 上有一个服务器,并且googollee/go-socket.io.当服务端和客户端工作在同一个端口时,sockets正常工作。但是当我开始它们在不同的端口上时,客户端会发生错误:WebSocket connection to 'ws://localhost:4444/socket.io/?EIO=3&transport=websocket&sid=6' failed: Error during WebSocket handshake: Unexpected response code: 403POST http://localhost:4444/socket.io/?EIO=3&transport=polling&t=NDDzcYM&sid=5 400 (Bad Request)GET http://localhost:4444/socket.io/?EIO=3&transport=polling&t=NDDzcYC&sid=5 400 (Bad Request)WebSocket connection to 'ws://localhost:4444/socket.io/?EIO=3&transport=websocket&sid=5' failed: Error during WebSocket handshake: Unexpected response code: 403在服务器上:connected: 1 closed client namespace disconnect meet error: json: cannot unmarshal object into Go value of type string connected: 2
1 回答

哈士奇WWW
TA贡献1799条经验 获得超6个赞
错误消息表明服务器尝试将您的 JSON 对象解组为字符串,但它预期会有所不同。
原因在于您的方法定义:
server.OnEvent("/", "msg", func(s socketio.Conn, msg string) string {
msg是字符串类型。
但是,然后您发送不同类型的消息:
public subscribe() {
this.socket.emit("msg", { msg: "Hello!" });
}
此消息可以在此结构定义中描述:
type Message struct {
Msg string `json:"msg"`
}
因此,将您的信息解组为字符串是行不通的,因为您发送的对象不是字符串,但必须以不同的方式表示。
要解决此问题,您有两种选择:
更改您的服务器端方法定义以能够接受客户端的数据:
使用
server.OnEvent("/", "msg", func(s socketio.Conn, msg Message) string {
,注意msg
现在是 typeMessage
,这是与接收到的数据匹配的结构。
或者
更改客户端发送的数据:
用于
this.socket.emit("msg", "Hello!");
仅发送一个字符串(然后将其正确解码为您msg
的字符串类型参数。
- 1 回答
- 0 关注
- 235 浏览
添加回答
举报
0/150
提交
取消