-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathws-browser.js
120 lines (100 loc) · 2.72 KB
/
ws-browser.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
/* eslint-env browser, commonjs */
'use strict';
const EventEmitter = require('events').EventEmitter;
const mdsf = require('mdsf');
const constants = require('./internal-constants');
const transportCommon = require('./transport-common');
// W3C WebSocket transport for JSTP.
// socket - WebSocket instance
// socketEventEmitter - an EventEmitter that proxies socket events
//
class Transport extends EventEmitter {
constructor(socket) {
super();
this.socket = socket;
this.socket.onmessage = message => {
this._onMessage(message);
};
['close', 'error'].forEach(event => {
this.socket.addEventListener(event, (...args) => {
this.emit(event, ...args);
});
});
}
// returns underlying socket.
//
getRawTransport() {
return this.socket;
}
// Send data over the connection.
// data - Buffer or string
//
send(data) {
if (Buffer.isBuffer(data)) {
data = data.toString();
}
this.socket.send(data);
}
// End the connection optionally sending the last chunk of data.
// data - Buffer or string (optional)
//
end(data) {
if (data) {
this.send(data);
}
this.socket.close();
}
// WebSocket message handler.
// message - WebSocket message
//
_onMessage(message) {
const data =
typeof message.data === 'string'
? message.data
: new Buffer(message.data).toString();
let parsed;
try {
parsed = mdsf.parse(data);
} catch (error) {
this.emit('error', error);
return;
}
this.emit('message', parsed);
}
}
// Create a JSTP client that will transfer data over a WebSocket connection.
// url - WebSocket endpoint URL
// appProvider - client application provider
//
const socketFactory = (url, callback) => {
try {
const webSocket = new WebSocket(url, constants.WEBSOCKET_PROTOCOL_NAME);
webSocket.onopen = () => {
callback(null, webSocket);
};
webSocket.onerror = callback;
} catch (error) {
if (callback) callback(error);
}
};
// see transportCommon.newConnectFn
//
const connect = transportCommon.newConnectFn(socketFactory, Transport, 'ws');
// see transportCommon.newConnectAndInspectFn
//
const connectAndInspect = transportCommon.newConnectAndInspectFn(
socketFactory,
Transport,
'ws'
);
// see transportCommon.newReconnectFn
//
const reconnect = transportCommon.newReconnectFn(socketFactory, Transport);
module.exports = {
Transport,
connect: (app, client, url, callback) => connect(app, client, url, callback),
connectAndInspect: (app, client, interfaces, url, callback) =>
connectAndInspect(app, client, interfaces, url, callback),
reconnect: (connection, url, callback) =>
reconnect(connection, url, callback),
};