forked from cordova-rtc/cordova-plugin-iosrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios-websocket-hack.js
242 lines (199 loc) · 4.81 KB
/
ios-websocket-hack.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* iOS Safari browser makes the Cordova app crash when any plugin method is called
* within a WebSocket event (such as onopen, onmessage, etc). This happens randomly.
* This script overrides the native WebSocket class by making all the events and
* methods to be fired within a setTimeout so event listeners are not fired in the
* buggy WebSocket context.
*
* The issue is described here:
* https://github.com/eface2face/cordova-plugin-iosrtc/issues/12
*
* USAGE:
*
* Just load this script in the HTML of your Cordova iOS app within the first
* <script> tag. Don't do it for Android since it's useless and this script
* deteriorates the WebSocket performance.
*/
(function () {
// Store a reference of the native WebSocket class.
var NativeWebSocket = window.WebSocket;
// Override native WebSocket and also expose the native one.
window.WebSocket = FakeWebSocket;
window.NativeWebSocket = NativeWebSocket;
// Fake WebSocket class that ill override the native one.
function FakeWebSocket() {
var self = this,
url = arguments[0],
protocols = arguments[1],
listeners = {};
// Create a native WebSocket instance.
if (protocols) {
this.ws = new NativeWebSocket(url, protocols);
} else {
this.ws = new NativeWebSocket(url);
}
// WebSocket is an EventTarget as per W3C spec.
this.addEventListener = function (type, newListener) {
var listenersType,
i, listener;
if (!type || !newListener) {
return;
}
listenersType = listeners[type];
if (listenersType === undefined) {
listeners[type] = listenersType = [];
}
for (i = 0; !!(listener = listenersType[i]); i++) {
if (listener === newListener) {
return;
}
}
listenersType.push(newListener);
};
this.removeEventListener = function (type, oldListener) {
var listenersType,
i, listener;
if (!type || !oldListener) {
return;
}
listenersType = listeners[type];
if (listenersType === undefined) {
return;
}
for (i = 0; !!(listener = listenersType[i]); i++) {
if (listener === oldListener) {
listenersType.splice(i, 1);
break;
}
}
if (listenersType.length === 0) {
delete listeners[type];
}
};
this.dispatchEvent = function (event) {
var self = this,
type,
listenersType,
dummyListener,
stopImmediatePropagation = false,
i, listener;
if (!(event instanceof Event)) {
throw new Error('first argument must be an instance of Event');
}
type = event.type;
listenersType = (listeners[type] || []);
dummyListener = this['on' + type];
if (typeof dummyListener === 'function') {
listenersType.push(dummyListener);
}
event.target = this;
for (i = 0; !!(listener = listenersType[i]); i++) {
if (stopImmediatePropagation) {
break;
}
fire(listener, event);
}
return !event.defaultPrevented;
function fire(listener, event) {
// Avoid iOS WebSocket bug by running the listener within a setTimeout.
setTimeout(function () {
listener.call(self, event);
});
}
};
// Set the native WebSocket events.
this.ws.onopen = function (event) {
setTimeout(function () {
self.dispatchEvent(event);
});
};
this.ws.onerror = function (event) {
setTimeout(function () {
self.dispatchEvent(event);
});
};
this.ws.onclose = function (event) {
setTimeout(function () {
self.dispatchEvent(event);
});
};
this.ws.onmessage = function (event) {
setTimeout(function () {
self.dispatchEvent(event);
});
};
}
// Expose W3C WebSocket attributes and setters.
Object.defineProperties(FakeWebSocket.prototype, {
url: {
get: function () {
return this.ws.url;
}
},
readyState: {
get: function () {
return this.ws.readyState;
}
},
protocol: {
get: function () {
return this.ws.protocol;
}
},
extensions: {
get: function () {
return this.ws.extensions;
}
},
bufferedAmount: {
get: function () {
return this.ws.bufferedAmount;
}
},
CONNECTING: {
get: function () {
return this.ws.CONNECTING;
}
},
OPEN: {
get: function () {
return this.ws.OPEN;
}
},
CLOSING: {
get: function () {
return this.ws.CLOSING;
}
},
CLOSED: {
get: function () {
return this.ws.CLOSED;
}
},
binaryType: {
get: function () {
return this.ws.binaryType;
},
set: function (type) {
this.ws.binaryType = type;
}
}
});
// Expose W3C WebSocket methods.
FakeWebSocket.prototype.send = function (data) {
var self = this;
// Avoid iOS WebSocket crash.
setTimeout(function () {
self.ws.send(data);
});
};
FakeWebSocket.prototype.close = function (code, reason) {
if (!code && !reason) {
this.ws.close();
} else if (code && !reason) {
this.ws.close(code);
} else {
this.ws.close(code, reason);
}
};
})();