-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (47 loc) · 1.58 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
var express = require('express');
var path=require('path');
var http = require('http');
var app=express();
//attaching socket io to our server
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'app')));
app.get('/', function(req, res){
res.sendFile(__dirname+'/index.html');
});
var users=[];
//an array of users have been created.
var httpServer=http.createServer(app);
var io = require('socket.io').listen(httpServer);
//instantiating a socket connection, handling all
//incomming connection from clients
io.sockets.on('connection', function(socket){
socket.on('user name',function(user_name){
users.push({id:socket.id,user_name:user_name});
len=users.length;
len--;
//Sending the user Id and List of users
io.emit('user entrance',users,users[len].id);
});
//collecting the message from a client
socket.on('chat message', function(data_server){
//emitting the signal again. telling the client to
//update the chat with the arguement data, but now
//we are going to add username.
socket
.broadcast
.to(data_server.id)
.emit(
'get msg',
{
msg:data_server.msg,
id:data_server.id,
senderid:data_server.senderid,
name:data_server.name
}
);
//broadcasting to a specific user with a user id in data_server
});
});
httpServer.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});