-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketio.js
93 lines (81 loc) · 2.22 KB
/
socketio.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
'use strict';
const socketio = require('socket.io');
const HermesMessage = require('hermesjs-message');
function init (settings) {
return function (hermes) {
return new HermesSocketIO(settings, hermes);
};
}
function HermesSocketIO (settings, hermes) {
this.hermes = hermes;
this.server_settings = settings;
}
/**
* Makes the adapter start listening.
*/
HermesSocketIO.prototype.listen = function listen () {
this.setup();
if (this.http_server.listen) {
this.server = this.http_server.listen(this.server_settings.port || 80);
} else {
this.server = this.http_server(this.server_settings.port || 80);
}
return this.server;
};
/**
* Setup adapter configuration.
*/
HermesSocketIO.prototype.setup = function setup () {
this.http_server = this.server_settings.http_server;
if (!this.http_server) {
this.io = socketio();
} else {
this.io = socketio(this.http_server);
}
this.io.on('connection', (socket) => {
this.hermes.emit('client:ready', { name: 'Socket.IO adapter' });
socket.onevent = (packet) => {
this.onReceiveFromClient(this.createMessage(packet, socket));
};
});
};
/**
* Sends the message coming from WS client to Hermes.
* @param {HermesMessage} message The message to be sent
*/
HermesSocketIO.prototype.onReceiveFromClient = function onReceiveFromClient (message) {
this.hermes.emit('client:message', message);
};
/**
* Serializes the WS message as an HermesMessage.
*
* @param {Object} packet WS message
* @param {Socket} client Socket object
* @return {HermesMessage}
*/
HermesSocketIO.prototype.createMessage = function createMessage (packet, client) {
const message = new HermesMessage({
topic: packet.data[0],
payload: packet.data[1] || {},
protocol: {
name: this.server_settings.protocol || 'ws',
headers: {
type: packet.type,
nsp: packet.nsp
}
},
connection: client,
packet
});
message.on('send', this.send.bind(this, message));
return message;
};
/**
* Sends the message down to the wire (WS client).
*
* @param {HermesMessage} message The message to be sent
*/
HermesSocketIO.prototype.send = function send (message) {
this.io.emit(message.topic, message.payload);
};
module.exports = init;