-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.js
42 lines (38 loc) · 907 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const WS = require('ws')
;((ws) => {
// debugger
const server = new WS.Server({ port: 9826 })
const init = () => {
bindEvent()
}
function bindEvent() {
server.on('open', handleOpen)
server.on('close', handleClose)
server.on('error', handleError)
server.on('connection', handleConnection)
}
function handleOpen() {
console.log('server websocket open')
}
function handleClose() {
console.log('server websocket close')
}
function handleError() {
console.log('server websocket error')
}
function handleConnection(ws) {
console.log('server websocket connection')
ws.on('message', handleMessage)
}
function handleMessage(msg) {
// console.log(server)
// debugger
server.clients.forEach((c) => {
//遍历客户端
// console.log(c)
c.send(msg) //发送
})
}
init()
console.log('连接成功')
})(WS)