如何实现回调功能
ctbsea opened this issue · 6 comments
Listener func(...any)
没有返回参数
怎么实现如下的回调功能
`
server.On("some:event", func(msg string) string {
return msg //Sending ack with data in msg back to client, using "return statement"
})
`
写了一个,可以用这个
type ClientMessage struct {
UserName string `json:"username"`
Payload string `json:"payload"`
}
func Wrapper(event string, client *socket.Socket, handler func(client *socket.Socket, cliMsg *entities.ClientMessage) []byte) func(...any) {
return func(datas ...any) {
logrus.Infof("recive a client event: [%s], datas: %+v", event, datas)
d, ok := datas[0].(string)
if !ok || d == "" {
logrus.Errorf("invalid data type: %+v", datas)
return
}
clientMsg := &entities.ClientMessage{}
if err := json.Unmarshal([]byte(d), clientMsg); err != nil {
logrus.WithError(err).Errorf("invalid data type: %+v", datas)
return
}
r := handler(client, clientMsg)
if len(r) > 0 {
client.Emit(event)
}
}
}
func EventHandler() {
server := socket.NewServer(nil, nil)
server.On("connection", func(clients ...any) {
client := clients[0].(*socket.Socket)
client.On("join", Wrapper("join", client, JoinEndpoint))
client.On("disconnect", func(...any) {})
})
}
func JoinEndpoint(client *socket.Socket, cliMsg *entities.ClientMessage) []byte {
// do your biz
}
写了一个,可以用这个
type ClientMessage struct { UserName string `json:"username"` Payload string `json:"payload"` } func Wrapper(event string, client *socket.Socket, handler func(client *socket.Socket, cliMsg *entities.ClientMessage) []byte) func(...any) { return func(datas ...any) { logrus.Infof("recive a client event: [%s], datas: %+v", event, datas) d, ok := datas[0].(string) if !ok || d == "" { logrus.Errorf("invalid data type: %+v", datas) return } clientMsg := &entities.ClientMessage{} if err := json.Unmarshal([]byte(d), clientMsg); err != nil { logrus.WithError(err).Errorf("invalid data type: %+v", datas) return } r := handler(client, clientMsg) if len(r) > 0 { client.Emit(event) } } } func EventHandler() { server := socket.NewServer(nil, nil) server.On("connection", func(clients ...any) { client := clients[0].(*socket.Socket) client.On("join", Wrapper("join", client, JoinEndpoint)) client.On("disconnect", func(...any) {}) }) } func JoinEndpoint(client *socket.Socket, cliMsg *entities.ClientMessage) []byte { // do your biz }
https://socket.io/zh-CN/docs/v4/client-api/#socketemiteventname-args-ack
Use this annotation example:
Lines 273 to 286 in 5758644
Lines 325 to 353 in 5758644
For more information, please check the documentation: https://pkg.go.dev/github.com/zishang520/socket.io/v2@v2.0.5/socket#Socket.Emit
Use this annotation example:
Lines 273 to 286 in 5758644
Lines 325 to 353 in 5758644
For more information, please check the documentation: https://pkg.go.dev/github.com/zishang520/socket.io/v2@v2.0.5/socket#Socket.Emit
上述提供的都是基于emit 主动发送消息后等待client ack
我是想实现on监听某个事件,当收到客户端消息的时候,处理完消息返回客户端一些消息,
官方案例
**Server**
io.on("connection", (socket) => {
socket.on("hello", (arg, callback) => {
console.log(arg); // "world"
callback("got it"); // **我问这个callback 对应的实现方式**
});
});
**Client**
socket.emit("hello", "world", (response) => {
console.log(response); // "got it"
});
Use this annotation example:
Lines 273 to 286 in 5758644
Lines 325 to 353 in 5758644
For more information, please check the documentation: https://pkg.go.dev/github.com/zishang520/socket.io/v2@v2.0.5/socket#Socket.Emit
上述提供的都是基于emit 主动发送消息后等待client ack
我是想实现on监听某个事件,当收到客户端消息的时候,处理完消息返回客户端一些消息,
官方案例 **Server** io.on("connection", (socket) => { socket.on("hello", (arg, callback) => { console.log(arg); // "world" callback("got it"); // **我想知道有没有对应的这个callback 对应的实现** }); }); **Client** socket.emit("hello", "world", (response) => { console.log(response); // "got it" });
client
socket.emit("hello", "world", (response) => {
console.log(response); // "i'm join"
});
go
io.Of("/", nil).On("connection", func(args ...any) {
socket := args[0].(*socket.Socket)
socket.On("join", func(a ...any) {
msg :=a[0] // "word"
// a[1]对应的是client 传递的 (response){}
a[1].(func([]interface{}, error))([]interface{}{"i'm join"}, nil)
})
})