-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1.33 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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server)
var port = process.env.PORT || 3000
server.listen(port, () => console.log(`listening on port ${ port }`))
var connections = []
io.on('connection', (socket) => {
console.log('Client connected');
socket.username = 'Rosalina';
socket.on('change username', (name) => socket.username = name)
socket.on('message', (msg) => io.emit('message',
{ 'user': socket.username, 'message': msg }))
socket.on('join', (username) => {
if (username != null) {
socket.username = username
}
console.log('user joined : ' + socket.username)
socket.broadcast.emit('message',
{ 'user': 'Serveur', 'message': socket.username + ' a rejoint le chat !'})
socket.emit('message',
{ 'user': 'Serveur', 'message': 'Bienvenue ' + socket.username + ' sur Rosalina. Il y a actuellement ' + connections.length + ' personne(s) connectée(s).'})
connections.push(socket.username)
})
socket.on('disconnect', () => {
console.log('user left : ' + socket.username)
socket.broadcast.emit('message',
{ 'user': 'Serveur', 'message': socket.username + ' a quitté le chat !'})
connections[socket.username] = null
})
})