Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.

Commit 7c0b368

Browse files
authored
Merge pull request #17 from python-paling:12-add-websocket-reconnect-functionality
Websocket reconnect
2 parents 493bcae + e0103b5 commit 7c0b368

9 files changed

Lines changed: 72 additions & 48 deletions

File tree

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

aal/aal.js renamed to paling/paling.js

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// aal.js
12
paling = {
23
_host: window.location.origin,
34

@@ -103,63 +104,86 @@ paling = {
103104
}
104105
},
105106

106-
_init: function () {
107-
paling._mock_py_functions();
107+
setWebsocketAutoReconnect: function (reconnect = false) {
108+
paling._websocket_auto_reconnect = reconnect;
109+
},
108110

109-
document.addEventListener("DOMContentLoaded", function (event) {
110-
let page = window.location.pathname.substring(1);
111-
paling._position_window(page);
111+
setWebsocketAutoReconnectTimeout: function (timeout = 1000) {
112+
paling._websocket_reconnect_timeout = timeout;
113+
},
112114

113-
let websocket_addr = (paling._host + '/paling').replace('http', 'ws');
114-
websocket_addr += ('?page=' + page);
115-
paling._websocket = new WebSocket(websocket_addr);
115+
_websocket_auto_reconnect: false,
116+
_websocket_reconnect_timeout: 1000,
116117

117-
paling._websocket.onopen = function () {
118-
for (let i = 0; i < paling._py_functions.length; i++) {
119-
let py_function = paling._py_functions[i];
120-
paling._import_py_function(py_function);
121-
}
118+
_init_websocket: function () {
119+
let page = window.location.pathname.substring(1);
120+
paling._position_window(page);
122121

123-
while (paling._mock_queue.length > 0) {
124-
let call = paling._mock_queue.shift();
125-
paling._websocket.send(paling._toJSON(call));
122+
let websocket_addr = (paling._host + '/paling').replace('http', 'ws');
123+
websocket_addr += ('?page=' + page);
124+
paling._websocket = new WebSocket(websocket_addr);
125+
126+
paling._websocket.onopen = function () {
127+
for (let i = 0; i < paling._py_functions.length; i++) {
128+
let py_function = paling._py_functions[i];
129+
paling._import_py_function(py_function);
130+
}
131+
132+
while (paling._mock_queue.length > 0) {
133+
let call = paling._mock_queue.shift();
134+
paling._websocket.send(paling._toJSON(call));
135+
}
136+
};
137+
138+
paling._websocket.onmessage = function (e) {
139+
let message = JSON.parse(e.data);
140+
if (message.hasOwnProperty('call')) {
141+
// Python making a function call into us
142+
if (message.name in paling._exposed_functions) {
143+
try {
144+
let return_val = paling._exposed_functions[message.name](...message.args);
145+
paling._websocket.send(paling._toJSON({ 'return': message.call, 'status': 'ok', 'value': return_val }));
146+
} catch (err) {
147+
debugger
148+
paling._websocket.send(paling._toJSON(
149+
{
150+
'return': message.call,
151+
'status': 'error',
152+
'error': err.message,
153+
'stack': err.stack
154+
}));
155+
}
126156
}
127-
};
128-
129-
paling._websocket.onmessage = function (e) {
130-
let message = JSON.parse(e.data);
131-
if (message.hasOwnProperty('call')) {
132-
// Python making a function call into us
133-
if (message.name in paling._exposed_functions) {
134-
try {
135-
let return_val = paling._exposed_functions[message.name](...message.args);
136-
paling._websocket.send(paling._toJSON({ 'return': message.call, 'status': 'ok', 'value': return_val }));
137-
} catch (err) {
138-
debugger
139-
paling._websocket.send(paling._toJSON(
140-
{
141-
'return': message.call,
142-
'status': 'error',
143-
'error': err.message,
144-
'stack': err.stack
145-
}));
146-
}
157+
} else if (message.hasOwnProperty('return')) {
158+
// Python returning a value to us
159+
if (message['return'] in paling._call_return_callbacks) {
160+
if (message['status'] === 'ok') {
161+
paling._call_return_callbacks[message['return']].resolve(message.value);
147162
}
148-
} else if (message.hasOwnProperty('return')) {
149-
// Python returning a value to us
150-
if (message['return'] in paling._call_return_callbacks) {
151-
if (message['status'] === 'ok') {
152-
paling._call_return_callbacks[message['return']].resolve(message.value);
153-
}
154-
else if (message['status'] === 'error' && paling._call_return_callbacks[message['return']].reject) {
155-
paling._call_return_callbacks[message['return']].reject(message['error']);
156-
}
163+
else if (message['status'] === 'error' && paling._call_return_callbacks[message['return']].reject) {
164+
paling._call_return_callbacks[message['return']].reject(message['error']);
157165
}
158-
} else {
159-
throw 'Invalid message ' + message;
160166
}
167+
} else {
168+
throw 'Invalid message ' + message;
169+
}
170+
171+
};
172+
173+
paling._websocket.onclose = function (e) {
174+
if (paling._websocket_auto_reconnect) {
175+
setTimeout(function () {
176+
paling._init_websocket();
177+
}, paling._websocket_reconnect_timeout);
178+
}
179+
};
180+
},
161181

162-
};
182+
_init: function () {
183+
paling._mock_py_functions();
184+
185+
document.addEventListener("DOMContentLoaded", function (event) {
186+
paling._init_websocket();
163187
});
164188
}
165189
};
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)