Create WS Server
1 | // get WS Object |
new WebSocket.Server(options[, callback])
1 | options: { |
Create a new server instance. One of port
, server
, or noServer
must be provided or an error is thrown.
Set VerifyClient
1 | function socketverify(info) { |
If verifyClient
is not set then the handshake is automatically accepted. If it is is provided with a single argument then that is:
1 | info: { |
If the verifyClient
provided with 2 arguments, the second one is cb
, which accepts result: boolean
, code: number
, name: string
.
Handle Request
1 | ws.on('connect', (socket) => { |
Events
Methods
Sending msg
1 | socket.send(data[, options[, callback]]) |
data: binary or string
options:
mask: true | false 是否使用掩码
binary: true | false 是否二进制流
compress: true | false 是否压缩
callback: 回调
close connection
1 | socket.close([code], [data]) |
code: 返回一个状态码
data: 结束连接时发送的信息
pause
1 | socket.pause() |
resume
1 | socket.resume() |
PING
1 | socket.ping([data], [options], [dontFailWhenClosed]) |
data: ping 的时候发送的信息
options: 同 send
dontFailWhenClosed: true | false // 如果连接断开, 是否抛出错误
PONG
1 | socket.pong([data], [options], [dontFailWhenClosed]) |
Ping 和 Pong 的区别在于, 一个包含 ping 帧(0x9), 一个包含 pong 帧(0xA)
pong 是对于接到 ping 之后的回应, 让双方都知道连接还存在