-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
29 lines (29 loc) · 1.24 KB
/
server.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
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);//modules: http, express, socket.io
app.use(express.static(__dirname + '/public'));//client file
//app.get('/', function (req, res) {//route
// res.sendfile(__dirname + '/public/Page1.html');
//});
var usernames = {};
io.sockets.on('connection', function (socket) {//three events: sendchat, adduser,disconnect
socket.on('sendchat', function (data) {
io.sockets.emit('chat', socket.username, data);//sends this methods
});
socket.on('adduser', function (username) {
socket.username = username;
usernames[username] = username;
socket.emit('chat', 'SERVER', 'you have connected');//sent to himself
socket.broadcast.emit('chat', 'SERVER', username + ' has connected');//sent to all users except who sent
io.sockets.emit('allusers', usernames);
});
socket.on('disconnect', function () {
delete usernames[socket.username];
io.sockets.emit('allusers', usernames);
socket.broadcast.emit('chat', 'SERVER', socket.username + ' has disconnected');
});
});
server.listen(1337);
console.log('Listening on port: 1337');