-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatServer.js
More file actions
115 lines (95 loc) · 2.88 KB
/
ChatServer.js
File metadata and controls
115 lines (95 loc) · 2.88 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var util = require('util');
var Writable = require('stream').Writable;
util.inherits(ChatServer, Writable);
function ChatServer(user, users, rooms) {
if (!(this instanceof ChatServer))
return new ChatServer(user, users, rooms);
Writable.call(this);
this._writableState.objectMode = true;
var isValidRoomName = function(roomName) {
return roomName && roomName.match(new RegExp(/^\w+$/)) != null;
};
var sendLineToRoom = function(roomName, line) {
if (!rooms[roomName]) return;
rooms[roomName].forEach(function(occupant) {
occupant.sendLine(line);
});
};
var roomRemoveOccupant = function(roomName, occupant) {
if (!rooms[roomName]) return false;
var i = rooms[roomName].indexOf(user);
if (i == -1) return false;
rooms[roomName].splice(i, 1);
if (rooms[roomName].length == 0) delete rooms[roomName];
return true;
};
var userLeaveRoom = function() {
var roomName = user.room;
user.room = null;
if(roomRemoveOccupant(roomName, user)) {
var leaveLine = '* user has left chat: ' + user.name;
user.sendLine(leaveLine + ' (** this is you)');
sendLineToRoom(roomName, leaveLine);
}
};
this.on('say', function(message) {
var roomName = user.room;
if (!rooms[roomName]) {
userLeaveRoom();
return;
}
sendLineToRoom(roomName, user.name + ': ' + message);
});
this.on('pm', function(message) {
if (!message) return;
var privateMessagePattern = new RegExp(/^(\w+)\s(.*)?$/);
var matches = message.match(privateMessagePattern);
if (!matches) return;
var userName = matches[1], message = matches[2];
if (!users[userName]) return;
users[userName].sendLine(user.name + ': ' + message);
});
this.on('rooms', function() {
user.sendLine('Active rooms are:');
for (var roomName in rooms) {
var occupants = rooms[roomName];
user.sendLine(' * ' + roomName + ' (' + occupants.length + ')');
}
user.sendLine('end of list.');
});
this.on('join', function(roomName) {
if(!isValidRoomName(roomName)) {
user.sendLine('Room name not accepted');
return;
}
userLeaveRoom();
sendLineToRoom(roomName, '* new user joined chat: ' + user.name);
if (!rooms[roomName]) {
//create room
rooms[roomName] = [];
}
rooms[roomName].push(user);
user.room = roomName;
//send room enter
user.sendLine('entering room: ' + roomName);
rooms[roomName].forEach(function(occupant) {
var selfMark = occupant == user ? ' (** this is you)' : '';
user.sendLine(' * ' + occupant.name + selfMark);
});
user.sendLine('end of list.');
});
this.on('leave', function() {
userLeaveRoom();
});
this.on('quit', function() {
userLeaveRoom();
delete users[user.name];
user.sendEnd('BYE');
console.log(user.name + ' signed out');
});
}
ChatServer.prototype._write = function(command, encoding, done) {
this.emit(command.command, command.payload);
done();
};
module.exports = ChatServer;