-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathchat.js
143 lines (124 loc) · 4.28 KB
/
chat.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var app = require('express')();
var http = require('http').Server(app);
var socket = require('socket.io')(http);
var port = 3421;
var customer = {
queue: [],
chat: [],
quit: [],
total: 0
};
socket.on('connection', function(client){
var username = false;
var isStaff = false;
client.on('login', function(data) {
username = data.username;
isStaff = data.isStaff;
});
client.on('subscribe', function(data) {
if (data.room == "staff_lobby")
{
// staff subscribe
}
else
{
// customer subscribe
if (! isStaff)
{
var exists = false;
for (var i=customer.queue.length-1; i>=0; i--) {
if (customer.queue[i].username === username)
{
exists = true;
customer.queue[i].join_at = new Date().toISOString();
break; //<-- Uncomment if only the first term has to be removed
}
}
if (exists === false)
{
customer.total++;
customer.queue.push({
id: customer.total,
username: username,
room: data.room,
status: 'new',
join_at: new Date().toISOString()
});
}
}
else
{
// if staff will update queue is talked
for (var i=customer.queue.length-1; i>=0; i--) {
if (customer.queue[i].username === data.room)
{
customer.queue[i].status = "staff talked";
break; //<-- Uncomment if only the first term has to be removed
}
}
}
client.join(data.room);
if (! isStaff)
{
socket.in(data.room).emit('message', {
sender: username,
message: username + " start private chat. You are "+ customer.total + ".",
send_at: new Date().toISOString(),
isStaff: isStaff
});
}
else
{
socket.in(data.room).emit('message', {
sender: username,
message: username + " start private chat.",
send_at: new Date().toISOString(),
isStaff: isStaff
});
}
socket.in("staff_lobby").emit('new_user', {});
}
console.log('joining room', username, data.room);
});
// note http://stackoverflow.com/questions/9418697/how-to-unsubscribe-from-a-socket-io-subscription
client.on('unsubscribe', function(data) {
// if staff will update queue is talked
for (var i=customer.queue.length-1; i>=0; i--) {
if (customer.queue[i].username === data.room)
{
customer.queue[i].status = "out";
break; //<-- Uncomment if only the first term has to be removed
}
}
// remove from queue
for (var i=customer.queue.length-1; i>=0; i--) {
if (customer.queue[i].username === username)
{
customer.queue.splice(i, 1);
break; //<-- Uncomment if only the first term has to be removed
}
}
console.log('leaving room', data.room);
socket.in(data.room).emit('message', {
sender: username,
message: username + " leave from this chat.",
send_at: new Date().toISOString()
});
client.leave(data.room);
});
client.on('send', function(data) {
console.log('sending message');
socket.in(data.room).emit('message', {
sender: username,
message: data.message,
send_at: new Date().toISOString(),
isStaff: isStaff
});
});
});
app.get('/customer/queue', function(req, res) {
res.json(customer.queue);
});
http.listen(port, function(){
console.log('listening on *:'+port);
});