-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (92 loc) · 2.91 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Setup basic express server
var jwt = require('jsonwebtoken');
var app = require("http");
var port = 8082;
var server = app.createServer().listen(port, process.env.IP);
var io = require('socket.io')(server);
var util = require('util');
var express = require('express');
var secret = 'E38A794745DB11459AC4565C6C417';
server.listen(port, function () {
console.log('Socket listening at port %d', port);
});
// Routing
app.use(express.static(__dirname + '/public'));
// Chatroom
var numUsers = 0;
io.on('connection', function (socket) {
var addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
io.to(socket.chatroom).emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'add user', this listens and executes
socket.on('add user', function (token) {
console.log('loft!');
var decoded;
try {
decoded = jwt.verify(token, secret);
}
catch(ex) {
console.log('decode error:' + ex);
socket.disconnect();
return;
}
console.log('decoded:['+ JSON.stringify(decoded) +']');
socket.chatroom = decoded.company.company.name;
socket.join(socket.chatroom);
console.log('joining to ' + socket.chatroom);
//io.to(socket.chatroom).emit( 'welcome, '+ decoded.login);
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = decoded.login;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers,
username: decoded.login,
room: decoded.company.company.name
});
var k=Array();
//console.log('broadcast:['+ util.inspect(socket.broadcast.nsp.sockets) +']');
for(var s in socket.broadcast.nsp.sockets) {
k.push(socket.broadcast.nsp.sockets[s].username);
console.log(socket.broadcast.nsp.sockets[s].id); //'socket id='+s.id);
}
//socket.broadcast.emit('user joined', {
io.to(socket.chatroom).emit('user joined', {
username: decoded.login,
numUsers: numUsers,
members: k,
protect: socket.id
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
if (addedUser) {
--numUsers;
// echo globally that this client has left
// socket.broadcast.emit('user left', {
io.to(socket.chatroom).emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});