This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathwebsockets.js
201 lines (175 loc) · 5.58 KB
/
websockets.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*****************************************************************************************
* (c) 2015-2021, Master Technology
* Licensed under the MIT license or contact me for a support, changes, enhancements,
* and/or if you require a commercial licensing
*
* Any questions please feel free to email me or put a issue up on github
*
* Version 2.0.0 [email protected]
****************************************************************************************/
"use strict";
const NativeWebSockets = require("./websockets-base");
/* global require, module, global */
// noinspection JSUnusedGlobalSymbols
/**
* This creates an "Browser" based Event for the Browser based WebSockets
* @param values {Object} - Object list of additional key items
* @constructor
*/
class BrowserWebSocketsEvent {
get bubbles() {
return false;
}
get cancelBubble() {
return false;
}
get cancelable() {
return false;
}
get defaultPrevented() {
return false;
}
get eventPhase() {
return false;
}
get path() {
return [];
}
get returnValue() {
return true;
}
constructor(values) {
this.timestamp = Date.now();
for (let key in values) {
if (values.hasOwnProperty(key)) {
this[key] = values[key];
}
}
}
/**
* This is the dummy preventDefault Function
* @returns {boolean}
*/
preventDefault() {
return false;
}
/**
* This is the dummy stopImmediatePropagation event
* @returns {boolean}
*/
stopImmediatePropagation() {
return false;
}
/**
* This is the dummy stopPropagation event
* @returns {boolean}
*/
stopPropagation() {
return false;
}
}
// noinspection DuplicatedCode
/**
* This is the Browser Based WebSocket creation factory
* @param url {string} - URL to connect to
* @param protocols {Array|String} - Protocols supported
* @returns {*} - WebSocket
* @constructor
*/
class BrowserWebSockets extends NativeWebSockets {
constructor(url, protocols) {
const options = {browser: true}
if (protocols != null) {
if (!Array.isArray(protocols)) {
options.protocols = [protocols];
} else {
options.protocols = protocols;
}
}
super(url, options);
this.binaryType = "arraybuffer";
this.onclose = null;
this.onerror = null;
this.onmessage = null;
this.onopen = null;
// We have to open this WS automatically, BUT we want this to fire after the rest of the users code does so that the user can attach his events
setTimeout( () => {
this.open();
}, 250);
}
/**
* This function is used to send the notifications back to the user code in the Browser webSocket mode
* @param event {String} - Event name ("message", "open", "close", "error")
* @param data {String|Array|ArrayBuffer} - The event Data
* @private
*/
_notify(event, data) {
let eventResult;
switch (event) {
case 'open':
eventResult = new BrowserWebSocketsEvent({
currentTarget: this,
srcElement: this,
target: this,
type: event
});
if (typeof this.onopen === "function") {
this.onopen.call(this, eventResult);
}
break;
case 'close':
eventResult = new BrowserWebSocketsEvent({
currentTarget: this,
srcElement: this,
target: this,
type: event,
code: data[1],
reason: data[2],
wasClean: data[3]
});
if (typeof this.onclose === "function") {
this.onclose.call(this, eventResult);
}
break;
case 'message':
eventResult = new BrowserWebSocketsEvent({
currentTarget: this,
srcElement: this,
target: this,
type: event,
data: data[1],
ports: null,
source: null,
lastEventId: ""
});
if (typeof this.onmessage === "function") {
this.onmessage.call(this, eventResult);
}
break;
case 'error':
eventResult = new BrowserWebSocketsEvent({
currentTarget: this,
srcElement: this,
target: this,
type: event,
error: data[1],
filename: "",
lineno: 0
});
if (typeof this.onerror === "function") {
this.onerror.call(this, eventResult);
}
break;
default:
return;
}
const eventCallbacks = this._callbacks[event];
for (let i = 0; i < eventCallbacks.length; i++) {
// noinspection JSUnresolvedFunction
this._callEventCallbacks(eventCallbacks[i].t || this, eventCallbacks[i].c, eventResult)
}
}
}
module.exports = NativeWebSockets;
// We attach to the GLOBAL object, so this is now available everywhere.
global.WebSocket = BrowserWebSockets;