-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
31 lines (25 loc) · 837 Bytes
/
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var db = [];
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
const user_connected = 'user connected'.fontcolor("green");
db.push(user_connected);
socket.emit('chat_message', db.join('<br>'));
socket.broadcast.emit('chat_message', user_connected);
socket.on('chat_message', function(msg){
db.push(msg);
socket.broadcast.emit('chat_message', msg);
});
socket.on('disconnect', function(){
const user_disconnected = 'user disconnected'.fontcolor("red");
db.push(user_disconnected);
socket.broadcast.emit('chat_message', user_disconnected);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});