-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (51 loc) · 1.8 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
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
var app = require('express')();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var webpack = require('webpack');
var config = require('./webpack.config.dev');
var compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
var onLineUsers = [];
var roomList = [ 'javascript', 'nodejs', 'reactjs', 'reactnative' ];
io.on('connection', function (socket) {
// listening login with userName and will broadcast the user joined message
// and also the onLineUsers list.
socket.on('login',function (userName) {
socket.userName = userName;
onLineUsers.push(socket.userName);
socket.emit('login',userName);
socket.broadcast.emit('user joined',`${userName} joined`);
socket.emit('roomlist', roomList);
// io.emit('userlist',onLineUsers);
});
socket.on('addroom', function (roomName) {
roomList.push(roomName);
socket.emit('roomlist', roomList);
});
// listening the chating message and broadcast to the room
socket.on('chat', function (msg) {
console.log('Socket %s say: %s', socket.userName,msg);
io.emit('chat',`${socket.userName}:${msg}`);
});
// when user left the room and
socket.on('disconnect', function () {
// check it
var indexOfUser = onLineUsers.indexOf(socket.userName);
var _tmp =
onLineUsers.slice(0,indexOfUser).concat(onLineUsers.slice(indexOfUser + 1));
onLineUsers = _tmp;
io.emit('userlist',onLineUsers);
socket.broadcast.emit('chat',`${socket.userName} disconnected`);
});
});
http.listen(3000, function () {
console.log('listening on: 3000');
});