-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.12 KB
/
index.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/static'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
nrUsers = 0;
io.on('connection', function (socket) {
socket.on('add user', function (username) {
socket.username = username;
++nrUsers;
io.emit('number users', {nrUsers: nrUsers});
});
socket.on('chat message', function (msg) {
io.emit('chat message', {message: msg.message, color: msg.color, username: socket.username});
});
socket.on('typing', function () {
socket.broadcast.emit('typing', {username: socket.username})
});
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {username: socket.username})
});
socket.on('disconnect', function () {
if (nrUsers > 0) {
--nrUsers;
}
io.emit('number users', {
nrUsers: nrUsers
});
});
});
http.listen(3000, function () {
console.log('listening on *:3000');
});