-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
25 lines (19 loc) · 723 Bytes
/
index.js
File metadata and controls
25 lines (19 loc) · 723 Bytes
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
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
io.on('connection', (socket) => {
socket.on('disconnect', function(){
io.emit('users-changed', {user: socket.nickname, event: 'left'});
});
socket.on('set-nickname', (nickname) => {
socket.nickname = nickname;
io.emit('users-changed', {user: nickname, event: 'joined'});
});
socket.on('add-message', (message) => {
io.emit('message', {text: message.text, from: socket.nickname, created: new Date()});
});
});
var port = process.env.PORT || 3001;
http.listen(port, function(){
console.log('listening in http://localhost:' + port);
});