-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (24 loc) · 817 Bytes
/
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
30
31
32
33
34
35
var socket_io = require('socket.io');
var http = require('http');
var express = require('express');
var app = express();
app.use(express.static('public'));
var server = http.Server(app);
var io = socket_io(server);
var users = 0;
io.on('connection', function (socket) {
users++;
socket.broadcast.emit('status', 'A new user has logged in');
io.sockets.emit('user_count', users);
socket.on('message', function(message) {
console.log('Received message:', message);
socket.broadcast.emit('message', message);
socket.emit('user_count', users);
});
socket.on('disconnect', function () {
users--;
socket.broadcast.emit('status', 'A user has logged out');
io.sockets.emit('user_count', users);
});
});
server.listen(8080);