触发备忘单
以下事件名称是保留的,不得在你的应用中使用:
¥The following event names are reserved and must not be used in your application:
connect
connect_error
disconnect
disconnecting
newListener
removeListener
// BAD, will throw an error
socket.emit("disconnecting");
服务器
¥Server
单一客户端
¥Single client
基本触发
¥Basic emit
io.on("connection", (socket) => {
socket.emit("hello", 1, "2", { 3: "4", 5: Buffer.from([6]) });
});
回执
¥Acknowledgement
- Callback
- Promise
io.on("connection", (socket) => {
socket.emit("hello", "world", (arg1, arg2, arg3) => {
// ...
});
});
io.on("connection", async (socket) => {
const response = await socket.emitWithAck("hello", "world");
});
确认和超时
¥Acknowledgement and timeout
- Callback
- Promise
io.on("connection", (socket) => {
socket.timeout(5000).emit("hello", "world", (err, arg1, arg2, arg3) => {
if (err) {
// the client did not acknowledge the event in the given delay
} else {
// ...
}
});
});
io.on("connection", async (socket) => {
try {
const response = await socket.timeout(5000).emitWithAck("hello", "world");
} catch (e) {
// the client did not acknowledge the event in the given delay
}
});
广播
¥Broadcasting
致所有已连接的客户端
¥To all connected clients
io.emit("hello");
除发送者外
¥Except the sender
io.on("connection", (socket) => {
socket.broadcast.emit("hello");
});
回执
¥Acknowledgements
- Callback
- Promise
io.timeout(5000).emit("hello", "world", (err, responses) => {
if (err) {
// some clients did not acknowledge the event in the given delay
} else {
console.log(responses); // one response per client
}
});
try {
const responses = await io.timeout(5000).emitWithAck("hello", "world");
console.log(responses); // one response per client
} catch (e) {
// some clients did not acknowledge the event in the given delay
}
在房间中
¥In a room
发送至名为 "我的房间" 的房间中所有已连接的客户端
¥to all connected clients in the room named "my room"
io.to("my room").emit("hello");
至除名为 "我的房间" 的房间中的客户端之外的所有已连接客户端
¥to all connected clients except the ones in the room named "my room"
io.except("my room").emit("hello");
有多个子句
¥with multiple clauses
io.to("room1").to(["room2", "room3"]).except("room4").emit("hello");
在命名空间中
¥In a namespace
io.of("/my-namespace").emit("hello");
修饰符绝对可以链接起来:
¥The modifiers can absolutely be chained:
io.of("/my-namespace").on("connection", (socket) => {
socket
.timeout(5000)
.to("room1")
.to(["room2", "room3"])
.except("room4")
.emit("hello", (err, responses) => {
// ...
});
});
这将向所有连接的客户端触发 "hello" 事件:
¥This will emit a "hello" event to all connected clients:
在名为
my-namespace
的命名空间中¥in the namespace named
my-namespace
至少在名为
room1
、room2
和room3
的房间之一中,但不在room4
中¥in at least one of the rooms named
room1
,room2
androom3
, but not inroom4
除发送者外
¥except the sender
并期待在接下来的 5 秒内收到确认。
¥And expect an acknowledgement in the next 5 seconds.
服务器之间
¥Between servers
基本触发
¥Basic emit
io.serverSideEmit("hello", "world");
接收方:
¥Receiving side:
io.on("hello", (value) => {
console.log(value); // "world"
});
回执
¥Acknowledgements
- Callback
- Promise
io.serverSideEmit("hello", "world", (err, responses) => {
if (err) {
// some servers did not acknowledge the event in the given delay
} else {
console.log(responses); // one response per server (except the current one)
}
});
try {
const responses = await io.serverSideEmitWithAck("hello", "world");
console.log(responses); // one response per server (except the current one)
} catch (e) {
// some servers did not acknowledge the event in the given delay
}
接收方:
¥Receiving side:
io.on("hello", (value, callback) => {
console.log(value); // "world"
callback("hi");
});
客户端
¥Client
基本触发
¥Basic emit
socket.emit("hello", 1, "2", { 3: "4", 5: Uint8Array.from([6]) });
回执
¥Acknowledgement
- Callback
- Promise
socket.emit("hello", "world", (arg1, arg2, arg3) => {
// ...
});
const response = await socket.emitWithAck("hello", "world");
确认和超时
¥Acknowledgement and timeout
- Callback
- Promise
socket.timeout(5000).emit("hello", "world", (err, arg1, arg2, arg3) => {
if (err) {
// the server did not acknowledge the event in the given delay
} else {
// ...
}
});
try {
const response = await socket.timeout(5000).emitWithAck("hello", "world");
} catch (e) {
// the server did not acknowledge the event in the given delay
}