@moddable/pebbleproxy 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/proxy.js +20 -7
package/package.json
CHANGED
package/proxy.js
CHANGED
|
@@ -20,13 +20,15 @@
|
|
|
20
20
|
|
|
21
21
|
console.log("moddable proxies launched");
|
|
22
22
|
|
|
23
|
+
const PROXY_VERSION = 1;
|
|
23
24
|
const HTTP_BASE = 15000;
|
|
25
|
+
const READY_MESSAGE = 15025;
|
|
24
26
|
const WS_BASE = 15050;
|
|
25
27
|
|
|
26
28
|
const gHTTPRequests = new Map;
|
|
27
29
|
const gWSRequests = new Map;
|
|
28
30
|
|
|
29
|
-
function
|
|
31
|
+
function appMessageReceived(e) {
|
|
30
32
|
if (state.log) {
|
|
31
33
|
console.log("moddable appmessage received");
|
|
32
34
|
|
|
@@ -49,7 +51,7 @@ function eventReceived(e) {
|
|
|
49
51
|
httpMessage(id, e);
|
|
50
52
|
}
|
|
51
53
|
catch (error) {
|
|
52
|
-
console.log("moddable
|
|
54
|
+
console.log("moddable proxy http exception" + error);
|
|
53
55
|
}
|
|
54
56
|
return true;
|
|
55
57
|
}
|
|
@@ -60,7 +62,7 @@ function eventReceived(e) {
|
|
|
60
62
|
wsMessage(id, e);
|
|
61
63
|
}
|
|
62
64
|
catch (error) {
|
|
63
|
-
console.log("moddable
|
|
65
|
+
console.log("moddable proxy ws exception" + error);
|
|
64
66
|
}
|
|
65
67
|
return true;
|
|
66
68
|
}
|
|
@@ -68,6 +70,14 @@ function eventReceived(e) {
|
|
|
68
70
|
return false;
|
|
69
71
|
};
|
|
70
72
|
|
|
73
|
+
function readyReceived(e) {
|
|
74
|
+
if (state.log)
|
|
75
|
+
console.log("readyReceived")
|
|
76
|
+
Pebble.sendAppMessage({
|
|
77
|
+
[READY_MESSAGE]: PROXY_VERSION
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
71
81
|
function httpMessage(id, e) {
|
|
72
82
|
if (state.log)
|
|
73
83
|
console.log(" connection: " + id);
|
|
@@ -250,7 +260,7 @@ function wsMessage(id, e) {
|
|
|
250
260
|
request.ws = new WebSocket(url);
|
|
251
261
|
request.ws.binaryType = "arraybuffer";
|
|
252
262
|
|
|
253
|
-
request.ws.onopen =
|
|
263
|
+
request.ws.onopen = ( )=> {
|
|
254
264
|
if (state.log)
|
|
255
265
|
console.log("websocket connected to host");
|
|
256
266
|
request.state = "connected";
|
|
@@ -261,7 +271,7 @@ function wsMessage(id, e) {
|
|
|
261
271
|
[WS_BASE + 2]: 0 // connected. success.
|
|
262
272
|
});
|
|
263
273
|
};
|
|
264
|
-
request.ws.onerror =
|
|
274
|
+
request.ws.onerror = () => {
|
|
265
275
|
if (state.log)
|
|
266
276
|
console.log("websocket connection failed");
|
|
267
277
|
request.state = "error";
|
|
@@ -283,11 +293,13 @@ function wsMessage(id, e) {
|
|
|
283
293
|
console.log(`close code ${code} reason ${arrayToString(reason)}`);
|
|
284
294
|
if (reason.byteLength)
|
|
285
295
|
bytes.set(arrayToUint8Array(reason.slice(2)), 2);
|
|
286
|
-
|
|
296
|
+
request.messages.push({
|
|
287
297
|
[WS_BASE + 1]: request.id,
|
|
288
298
|
[WS_BASE + 3]: 0, // disconnected clean.
|
|
289
299
|
[WS_BASE + 10]: Array.from(bytes) // sendAppMessage wants an Array
|
|
290
300
|
});
|
|
301
|
+
if (!request.messages.sending)
|
|
302
|
+
sendRequestMessage(request);
|
|
291
303
|
};
|
|
292
304
|
request.ws.onmessage = event => {
|
|
293
305
|
let data = event.data; // either ArrayBuffer or String
|
|
@@ -472,7 +484,8 @@ function stringToArray(str) {
|
|
|
472
484
|
}
|
|
473
485
|
|
|
474
486
|
const state = {
|
|
475
|
-
|
|
487
|
+
appMessageReceived,
|
|
488
|
+
readyReceived,
|
|
476
489
|
log: false
|
|
477
490
|
};
|
|
478
491
|
|