-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (33 loc) · 1.2 KB
/
Copy pathserver.js
File metadata and controls
39 lines (33 loc) · 1.2 KB
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
const express = require('express')
const SocketServer = require('ws').Server
//指定開啟的 port
const PORT = 3000
//創建 express 的物件,並綁定及監聽 3000 port ,且設定開啟後在 console 中提示
const server = express()
.listen(PORT, () => console.log(`Listening on ${PORT}`))
//將 express 交給 SocketServer 開啟 WebSocket 的服務
const wss = new SocketServer({ server })
//當 WebSocket 從外部連結時執行
wss.on('connection', ws => {
//連結時執行此 console 提示
console.log('Client connected')
//固定送最新時間給 Client
// const sendNowTime = setInterval(()=>{
// ws.send('ping')
// },1000)
//對 message 設定監聽,接收從 Client 發送的訊息
ws.on('message', (data) => {
// //data 為 Client 發送的訊息,現在將訊息原封不動發送出去
// ws.send(data)
//取得所有連接中的 client
let clients = wss.clients
//做迴圈,發送訊息至每個 client
clients.forEach(client => {
client.send(data)
})
})
//當 WebSocket 的連線關閉時執行
ws.on('close', () => {
console.log('Close connected')
})
})