-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (146 loc) · 5.3 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var visitors = [];
var visitorNames = {};
var operator = null;
var operatorChattingWith = null;
var removeVisitor = function(socketId) {
var position = getVisitorPosition(socketId);
if (position === -1) {
return;
}
visitors.splice(position, 1);
delete visitorNames[socketId];
};
var addVisitor = function(socketId, name) {
visitors.push(socketId);
visitorNames[socketId] = name;
};
var getVisitorPosition = function(socketId) {
return visitors.indexOf(socketId);
}
var removeOperator = function(socketId) {
operator = null;
};
var addOperator = function(socketId) {
operator = socketId;
};
var handleVisitorDisconnect = function(socket) {
// If this was the current active chat target, move on to next visitor.
if (operatorChattingWith === socket.id) {
var newVisitorPosition = getVisitorPosition(socket.id) + 1;
operatorChattingWith = visitors[newVisitorPosition] || null;
// Tell next user they're up
if (operatorChattingWith) {
socket.to(operatorChattingWith).emit('update-operator-status', true);
socket.to(operatorChattingWith).emit(
'update-visitor-queue', getVisitorPosition(operatorChattingWith)
);
}
}
removeVisitor(socket.id);
socket.to(operator).emit(
'update-chatting-with', operatorChattingWith, visitorNames
);
socket.to(operator).emit('update-visitors-list', visitors, visitorNames);
};
var handleOperatorDisconnect = function(socket) {
removeOperator(socket.id);
operatorChattingWith = null;
io.emit('update-operator-status', false);
io.emit('chat-ended', 'The operator has ended this session.');
};
var updateVisitorQueuePosition = function(socket, visitors) {
visitors.forEach(function(visitor) {
return socket.to(visitor).emit(
'update-visitor-queue', getVisitorPosition(visitor)
);
});
};
// Static assets
app.use('/css', express.static(__dirname + '/public/css'));
// Routes
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/operator', function(req, res) {
res.sendFile(__dirname + '/operator.html');
});
// Event handling
io.on('connection', function(socket) {
socket.on('disconnect', function() {
if (socket.id === operator) {
// Operator disconnected
handleOperatorDisconnect(socket);
} else {
// Visitor disconnected
handleVisitorDisconnect(socket);
updateVisitorQueuePosition(socket, visitors);
}
});
socket.on('visitor-joined-chat', function(name) {
addVisitor(socket.id, name);
if (getVisitorPosition(socket.id) === 0 && operator) {
// Start chat with this visitor
socket.emit('update-operator-status', true);
operatorChattingWith = socket.id;
socket.to(operator).emit(
'update-chatting-with', operatorChattingWith, visitorNames
);
}
socket.emit('update-visitor-queue', getVisitorPosition(socket.id));
socket.to(operator).emit('update-visitors-list', visitors, visitorNames);
});
socket.on('operator-joined-chat', function() {
addOperator(socket.id);
socket.to(visitors[0]).emit('update-operator-status', true);
operatorChattingWith = visitors[0];
socket.emit('update-chatting-with', operatorChattingWith, visitorNames);
socket.emit('update-visitors-list', visitors, visitorNames);
});
socket.on('message-to-operator', function(message) {
// Send message operator and visitor
socket.to(operator).emit(
'message-to-operator', visitorNames[socket.id], message
);
socket.emit('message-to-operator', visitorNames[socket.id], message);
});
socket.on('message-to-visitor', function(message) {
if (message !== '!next') {
// Send message to the visitor and operator
socket.to(operatorChattingWith).emit(
'message-to-visitor', 'operator', message
);
socket.emit('message-to-visitor', 'operator', message);
return;
}
// If message was '!next', go to next visitor in queue
var previousActiveVisitor = operatorChattingWith;
// Update current value of who operator is chatting with
var newVisitorPosition = getVisitorPosition(previousActiveVisitor) + 1;
operatorChattingWith = visitors[newVisitorPosition] || null;
socket.to(previousActiveVisitor).emit('update-operator-status', false);
socket.to(previousActiveVisitor).emit(
'chat-ended', 'The operator has ended this session.'
);
socket.emit(
'chat-ended', 'You have ended the chat with '
+ visitorNames[previousActiveVisitor]);
removeVisitor(previousActiveVisitor);
socket.emit('update-visitors-list', visitors, visitorNames);
socket.emit('update-chatting-with', operatorChattingWith, visitorNames);
// Tell next user they're up if user exists
if (operatorChattingWith) {
socket.to(operatorChattingWith).emit('update-operator-status', true);
socket.to(operatorChattingWith).emit(
'update-visitor-queue', getVisitorPosition(operatorChattingWith)
);
updateVisitorQueuePosition(socket, visitors);
}
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});