-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
28 lines (25 loc) · 884 Bytes
/
app.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
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({
port: 9527,
});
wss.on("connection", function connection(ws) {
console.log("客户端连接成功");
ws.on("message", function message(data) {
let message = JSON.parse(data.toString())
switch (message.action) {
case 'message':
console.log(getTime(), message.message.user_nickName + ':' + message.message.msg_content)
break
case 'join':
console.log(getTime(), message.message.user_nickName + ':' + message.message.msg_content)
break
}
wss.clients.forEach(cen => {
cen.send(JSON.stringify(message))
})
});
});
function getTime() {
return `[${new Date().toLocaleTimeString()}]`
}
console.log('打开-> http://127.0.0.1:9527')