-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatServer.js
26 lines (24 loc) · 1 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
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(8124);
function handler (req, res) {
fs.readFile(__dirname + '/chat.html', function (err, data) {
if (err) { res.writeHead(500);
return res.end('Error loading chat.html'); }
res.writeHead(200);
res.end(data); });
}
io.sockets.on('connection', function (socket) {
socket.on('addme',function(username) {
socket.username = username;
socket.emit('chat', 'SERVER', 'Hi '+ username+' You have connected');
socket.broadcast.emit('chat', 'SERVER', username + ' is on deck');
});
socket.on('sendchat', function(data) {
io.sockets.emit('chat', socket.username, data);
});
socket.on('disconnect', function() {
io.sockets.emit('chat', 'SERVER', socket.username + ' has left the building');
});
});