ha
Search…
关于本socket.io中文文档
socket.IO website
1 指南(Guide)
2 文档(Docs)
2.1 概览
2.1.1 啥是Socket.io
2.1.2 Socket.io不是什么
2.1.3 安装
2.1.4 在Node HTTP 服务中使用
2.1.5 在express中使用Socket.io
2.1.6 发送和接收事件
2.1.7 限制自己使用命名空间
2.1.8 发送易失性的消息
2.1.9 发送和获取数据(确认)
2.1.10 广播消息
2.1.111 作为跨浏览器使用websocket
2.2 房间和命名空间
2.3 从0.9迁移版本 migrating_from_0.9
2.4 多路节点使用
2.5 日志和调试
2.6 emit 备忘单
2.7 内部概述
2.8 faq
3 客户端-API(Client-API)
4 服务端-API(Server-API)
Powered By
GitBook
2.1.9 发送和获取数据(确认)
有时,您可能希望在客户端确认消息接收时收到回调。
为此,只需将函数作为
.send
或
.emit
的最后一个参数传递。 更重要的是,当你使用
.emit
时,确认是由你完成的,这意味着你也可以传递数据:
Server(app.js)
1
const
io
=
require
(
'socket.io'
)(
80
)
2
io
.
on
(
'connection'
,
socket
=>
{
3
socket
.
on
(
'ferret'
,(
name
,
word
,
fn
)
=>
{
4
fn
(
name
+
'says'
+
word
)
5
})
6
})
Copied!
client(index.html)
1
<
script
>
2
const
socket
=
io
()
3
socket
.
on
(
'connect'
,()
=>
{
4
socket
.
emit
(
'ferrect'
,
'tobi'
,
'woot'
,(
data
)
=>
{
5
console
.
log
(
data
)
//应该是 'tobi says woot'
6
})
7
})
8
</
script
>
Copied!
Previous
2.1.8 发送易失性的消息
Next
2.1.10 广播消息
Last modified
2yr ago
Copy link
Contents
Server(app.js)
client(index.html)