-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_packet.js
More file actions
99 lines (86 loc) · 2.41 KB
/
server_packet.js
File metadata and controls
99 lines (86 loc) · 2.41 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Packet class.
Example use of chaining: new Packet().acceptJoin().fullText(room).clientList(room).Send(client);
*/
exports.Packet = function () {
const data = {};
//reject_join, sent to clients who have been rejected from a room for some @reason
this.Reject = function (reason) {
data.reject = {};
data.reject.reason = reason;
return this;
};
//new_member, inform room members of a new member.
this.newMember = function (client, room) {
data.new_member = client.info;
data.member_count = room.member_count;
data.members = room.getMembersInfo();
return this;
};
this.memberLeft = function (client, room, reason) {
data.member_left = client.info;
if (room) {
data.member_count = room.member_count;
data.members = room.getMembersInfo();
data.reason = reason;
}
return this;
};
//accept_join, inform a client that their request to join has been accepted.
this.acceptJoin = function (client, is_newroom) {
data.accept_join = {
info: client.info,
};
data.new_room = is_newroom;
data.member_count = client.data.room.member_count;
data.members = client.data.room.getMembersInfo();
return this;
};
this.nameChange = function (client, oldname, you) {
data.name_change = {
client: client.info,
oldname: oldname,
you: you,
};
data.members = client.data.room.getMembersInfo();
return this;
};
this.Chat = function (client, msg) {
data.chat = {
sender: client.info,
text: msg,
};
return this;
};
this.chatHistory = function (history) {
data.chathistory = history;
return this;
};
this.Set = function (name, ndata) {
data[name] = ndata;
return this;
};
///////////////////////////////
//Send this packet to a client;
this.Send = function (client) {
client.send(data);
};
//Send this packet to all clients other than @exclude
this.Broadcast = function (socket, exclude) {
if (exclude) {
socket.broadcast(data, exclude.sessionId);
} else {
socket.broadcast(data);
}
};
//Send this packet to all clients in @room other than @exclude
this.broadcastToRoom = function (room, exclude) {
var i;
if (!room) return; //TODO: This should never happen, but it does. Find out why.
var dest = room.getMembers();
for (i in dest) {
if (dest[i] !== exclude) {
dest[i].send(data);
}
}
};
};