Comments (6)
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)
})
})
from socket.io.
写了一个,可以用这个
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
}
from socket.io.
写了一个,可以用这个
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
我期望的是这种效果,你提供的代码再客户端无法直接通过response 获取到消息
from socket.io.
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/[email protected]/socket#Socket.Emit
from socket.io.
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/[email protected]/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"
});
from socket.io.
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/[email protected]/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" });
from socket.io.
Related Issues (20)
- please add example list ,for example "gin", or export a socketio serverMux HOT 5
- how to use middle in Use funtion, stop this downward bubbling or next HOT 3
- 什么时候出4.6兄台 持支持支 HOT 3
- Any example ? HOT 3
- Benchmarking HOT 3
- Is *socket.Server Emit() safe for concurrent use? HOT 1
- Start https socket server HOT 1
- 怎么添加这个库为依赖 HOT 5
- How to get the clients or one of them HOT 2
- Does this project have Discord or Telegram group? HOT 1
- Golang client HOT 6
- ack 回调中的err参数具体是什么?有没有例子? HOT 8
- Possible to Ack incomming client EmitWithAck messages? HOT 1
- how to use on echo web freamwork HOT 2
- Nothing, absolutely nothing. works. Not even the default samples.
- Do you mind looking into Fiber adaptor for net/http interface to make this library work with fiber applications? HOT 4
- EmitwithAck never listens for the callback from the client so it always timed out HOT 6
- how to fetch /socket.io/socket.io.js ? HOT 3
- 请问socketio的reconnect机制到底啥玩意 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from socket.io.