-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatServer.js
More file actions
65 lines (51 loc) · 1.74 KB
/
chatServer.js
File metadata and controls
65 lines (51 loc) · 1.74 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
// Load the TCP Library
net = require('net');
// Keep track of the chat clients
var clients = [];
// Start a TCP Server
net.createServer(function(socket) {
// Identify this client
var address = socket.remoteAddress === '::1' ? "localhost" : socket.remoteAddress;
socket.name = address + ":" + socket.remotePort
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n\r");
broadcast("\n\r" + socket.name + " joined the chat" + "\n\r", socket);
// my change here....
var message = '';
// Handle incoming messages from clients.
socket.on('data', function(data) {
// copy incoming data to message
message += data
var n = message.indexOf('\n')
// if we have a \n? in message then emit one or more 'line' events
while (~n) {
socket.emit('line', message.substring(0, n))
message = message.substring(n + 1)
n = message.indexOf('\n')
}
});
// Broadcast on end of line
socket.on('line', function() {
broadcast(socket.name + "> " + message, socket);
message = '';
})
// Remove the client from the list when it leaves
socket.on('end', function() {
clients.splice(clients.indexOf(socket), 1);
broadcast("\n\r" + socket.name + " left the chat." + "\n\r");
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function(client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}
}).listen(5000);
// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5000\n");