-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
162 lines (130 loc) · 4.43 KB
/
socket.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
"use strict"
// Inter-process notifications
// Luke Mitchell, 13/06/2016
let SOCKET_PORT = process.env.SOCKET_PORT || 3000;
let LOG_PREFIX = process.env.LOG_PREFIX || '';
let EVENT_NAME_WILDCARD = '*';
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
// Object containing subscribers to events
let subscriptions = {};
// Object containing routines to call
let routines = {};
// Set up the socket
server.listen(SOCKET_PORT, function () {
console.log(LOG_PREFIX + '[SOCKET] Listening at port ' + SOCKET_PORT);
});
// Call a routine, returning the promise
function call(routineName, args) {
if (routines[routineName]) {
console.log(LOG_PREFIX + '[SOCKET] Calling routine with name:', routineName);
return routines[routineName](args);
}
};
// Add a new routine
function addRoutine(routineName, fn) {
console.log(LOG_PREFIX + '[SOCKET] Adding routine to:', routineName);
routines[routineName] = fn;
};
// Remove a routine
function removeRoutine(routineName) {
console.log(LOG_PREFIX + '[SOCKET] Removing routine from:', routineName);
delete routines[routineName];
};
// Create a subscription
function subscribe(eventName, socket) {
if (eventName == EVENT_NAME_WILDCARD) {
Object.keys(subscriptions).forEach(key => {
subscribe(key, socket);
});
} else {
// New list?
if (!subscriptions[eventName]) {
subscriptions[eventName] = [];
}
// Is the subscription already present?
let existing = subscriptions[eventName].find(obj => {
return (obj == socket);
});
if (!existing) {
subscriptions[eventName].push(socket);
}
}
};
// Delete a subscription
function unsubscribe(eventName, socket) {
if (eventName == EVENT_NAME_WILDCARD) {
Object.keys(subscriptions).forEach(key => {
unsubscribe(key, socket);
});
} else {
// Empty list?
if (!subscriptions[eventName]) return;
// Delete the subscription using the filter, recreatign the array
subscriptions[eventName] = subscriptions[eventName].filter(obj => {
return (obj !== socket);
})
// Delete the list if it's empty
if (subscriptions[eventName].length == 0) {
delete subscriptions[eventName];
}
}
};
// Send a message to all subscribers
function publish(eventName, message) {
if (!subscriptions[eventName]) return;
let data = JSON.stringify(message);
console.log(LOG_PREFIX + '[SOCKET] Publishing to \'' + eventName + '\'. There are ' + subscriptions[eventName].length + ' subscriptions.');
subscriptions[eventName].forEach(conn => {
conn.emit(eventName, data);
});
}
// Events fired for each new connection
io.on('connection', socket => {
console.log(LOG_PREFIX + '[SOCKET] New connection from ' + socket.client.conn.remoteAddress);
socket.on('call', (data, fn) => {
let args = data.args || {};
let routineName = data.routineName;
if (!routineName) {
console.error(LOG_PREFIX + '[SOCKET] routineName not provided');
return fn({ message: 'routineName not provided' }, false);
}
console.log(LOG_PREFIX + '[SOCKET] Received call command for:', routineName);
call(routineName, args)
.then(result => {
console.log(LOG_PREFIX + '[SOCKET] Call command was resolved with value:', result);
return fn(null, result);
})
.catch(err => {
console.error(LOG_PREFIX + '[SOCKET] Call command was rejected with error:', err);
return fn(err, false)
});
});
socket.on('subscribe', data => {
let eventName = data.eventName;
if (!eventName) {
return console.error(LOG_PREFIX + '[SOCKET] eventName not provided');
}
console.log(LOG_PREFIX + '[SOCKET] Adding subscription to:', eventName);
subscribe(eventName, socket);
});
socket.on('unsubcribe', data => {
let eventName = data.eventName;
if (!eventName) {
return console.error(LOG_PREFIX + '[SOCKET] eventName not provided');
}
console.log(LOG_PREFIX + '[SOCKET] Removing subscription from:', eventName);
unsubscribe(eventName, socket);
});
socket.on('disconnect', function() {
console.log(LOG_PREFIX + '[SOCKET] Client disconnected');
unsubscribe(EVENT_NAME_WILDCARD, socket);
});
});
module.exports.SOCKET_PORT = SOCKET_PORT;
module.exports.EVENT_NAME_WILDCARD = EVENT_NAME_WILDCARD;
module.exports.routines = {};
module.exports.routines.add = addRoutine;
module.exports.routines.remove = removeRoutine;
module.exports.publish = publish;