-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
48 lines (36 loc) · 929 Bytes
/
client.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
43
44
45
46
const io = require('socket.io-client');
// 连接到 Socket.io 服务器
const socket = io('http://localhost:3000'); // 将服务器地址替换为你的服务器地址
// 监听服务器发送的消息
// 发送消息到服务器
function message(id,msg){
socket.emit('message', id,msg);
}
socket.on('message', (msg) => {
console.log(msg);
});
// 处理断开连接事件
function disconnect(){
socket.disconnect();
}
socket.on('disconnect', () => {
console.log('与服务器的连接已断开');
});
function joinGroup(name,id){
socket.emit('joinGroup', name,id);
}
socket.on('joinGroup', (msg) => {
console.log(`收到消息: ${msg}`);
});
function getList(id){
socket.emit('getList', id);
}
socket.on('getList', (list) => {
console.log(list);
});
function updateList(id,list){
socket.emit('updateList', id,list);
}
socket.on("updateList", (msg) => {
console.log(`收到消息: ${msg}`);
});