-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (61 loc) · 1.99 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
app.use(express.static(__dirname));
app.get('/', function(req, res){
res.sendfile('./index.html');
});
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
var clients = new Array();
io.on('connection', function(socket){
var addedUser = false;
socket.on('chat message', function(msg,number){
var index = clients.indexOf(socket.username);
if(index > -1)
{
console.log(socket.username + " answered on questeion " + number + ": " + msg);
socket.broadcast.emit('new message', {
username: socket.username,
message: msg
});
}
});
socket.on('disconnect', function () {
var index = clients.indexOf(socket.username);
if(index > -1)
{
clients.splice(index, 1);
}
console.log('user left:' + socket.username);
console.log('clients: ' + clients);
// echo globally that this client has left
});
socket.on('add user', function (username) {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
var index = clients.indexOf(socket.username);
if(index > -1){
clients[index] = socket.username;}
else{
clients.push(socket.username);
}
addedUser = true;
console.log(socket.username + " joined");
console.log('clients: ' + clients);
socket.emit('login', {
});
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
isocket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
});
});