This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_node_sample_server.js
184 lines (149 loc) · 5.52 KB
/
plugin_node_sample_server.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
172
173
174
175
176
177
178
179
180
181
182
183
184
// ┌────────────────────────────────────────────────────────────────────┐ \\
// │ freeboard.io-node.js │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Copyright © 2014 Hugo Sequeira (https://github.com/hugocore) │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Licensed under the MIT license. │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Simple node.js and sockets.io server to test the node.js plugin. │ \\
// └────────────────────────────────────────────────────────────────────┘ \\
/*
* Configurations and helpers
*/
var namespace = '/cc';
var room = 'queues';
var refreshTimer = 2000;
var connectionscounter = 0;
var eventNames = ['showQueues', 'showAgentMissed'];
var serverport = 8989;
var ans1 = 0;
var miss1 = 0;
var ans2 = 0;
var miss2 = 0;
var sla = 0;
var ag = [0,0,0,0,0];
/*
* Project dependencies
*/
var io = require('socket.io')(serverport);
/*
* Collects data
*/
// Implement the methods to handle the new events
function newEventCallback(eventName, message) {
// Construct the json object to be propagated
/*
var json = {
"send_queues": [
{
queue: message.queue,
answered: message.answered,
missed: message.missed
}]
};
*/
// Invokes propagation
propagatesEvent(eventName, JSON.stringify(message));
}
// Connection to external data sources
function connectToExternalSources() {
// Simulate the connection and new messages with a timer function
setInterval(function () {
// construct message
var oneHourInMilis = 3600000;
var message = {
"header": [
{
Name: "Name",
Answered: "Answered",
Missed: "Missed",
SLA: "SLA"
}
],
"data": [
{
Name: "sales",
Answered: ans1 = ans1 + Math.floor(Math.random() * 5),
Missed: miss1 = miss1 + Math.floor(Math.random() * 2),
SLA: Math.floor(Math.random() * (100 - 70) + 70)
},
{
Name: "support",
Answered: ans2 = ans2 + Math.floor(Math.random() * 5),
Missed: miss2 = miss2 + Math.floor(Math.random() * 2),
SLA: Math.floor(Math.random() * (100 - 70) + 70)
}
]
};
newEventCallback('showQueues', message);
var oneHourInMilis = 3600000;
var message = {
"header": [
{
Name: "Name",
Answered: "Answered"
}
],
"data": [
{
Name: "Dzon Rambo",
Answered: ag[0] = ag[0] + Math.floor(Math.random() * 10)
},
{
Name: "Angelina Jolda",
Answered: ag[1] = ag[1] + Math.floor(Math.random() * 3)
},
{
Name: "Donald Dump",
Answered: ag[2] = ag[2] + Math.floor(Math.random() * 2)
},
{
Name: "Pretty Woman",
Answered: ag[3] = ag[3] + Math.floor(Math.random() * 3)
},
{
Name: "Chuck Norris",
Answered: ag[4] = ag[4] + Math.floor(Math.random() * 10)
}
]
};
newEventCallback('agentsAnswered', message);
}, refreshTimer);
}
/*
* Data propagation
*/
// Propagates event through all the connected clients
function propagatesEvent(eventName, event) {
if (connectionscounter > 0) {
io.of(namespace).to(room).emit(eventName, event);
console.log("New event propagated in: Namespace='%s' Room='%s' EventName='%s' Event='%s'", namespace, room, eventName, event);
}
}
/*
* Handle Sockets.io connections
*/
// Event handlers
io.of(namespace).on('connection', function (socket) {
console.log("New client connected.");
// Do some logic for every new connection
connectionscounter++;
// On subscribe events join client to room
socket.on('subscribe', function (room) {
socket.join(room);
console.log("Client joined room: " + room);
});
// On disconnect events
socket.on('disconnect', function (socket) {
console.log("Client disconnect from rooms.");
connectionscounter--;
});
});
/*function puts(message) {
console.log(message);
}*/
/*
* Run
*/
console.log("Starting Node.js server with namespace='%s' and room='%s'", namespace, room);
connectToExternalSources();