-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.js
60 lines (44 loc) · 1.34 KB
/
chatserver.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var http = require('http'),
url = require('url'),
fs = require('fs'),
server
server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
switch(path){
case '/':
fs.readFile(__dirname + '/chatclient.html', function(err, data){
if(err) return send404(res);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data, 'utf8');
res.end();
});
break;
default: send404(res);
}
}),
send404 = function(res){
res.writeHead(404);
res.write('404');
res.end();
};
server.listen(3000, "127.0.0.1");
console.log('Server Running at http://127.0.0.1:8080/');
var io = require('socket.io').listen(server);
//on 'connection' event
io.sockets.on('connection', function(socket){
console.log("Connection "+ socket.id + " accepted.");
socket.on('disconnect', function(){
console.log("Connection "+ socket.id + " terminated");
});
socket.on('message', function(message){
console.log("Received Message: " + message.messsage + " - from client " + message.username);
//Send the messages to all connected clients.
io.sockets.emit("chat", socket.id , message);
});
socket.on('username', function(username){
console.log("THE USERNAME IS " + username + " ON SOCKET ID" + socket.id);
//Send the messages to all connected clients.
io.sockets.emit('username', socket.id , username);
})
});
//});