Skip to content

Commit b3e46e4

Browse files
authored
Updated Websocket connection
Added support for Websocket connections through proxy server.
1 parent e9e2660 commit b3e46e4

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

vpython/vpython_libraries/glowcomm.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
define(["nbextensions/vpython_libraries/plotly.min",
1+
define(["base/js/utils",
2+
"nbextensions/vpython_libraries/plotly.min",
23
"nbextensions/vpython_libraries/glow.min",
3-
"nbextensions/vpython_libraries/jquery-ui.custom.min"], function(Plotly) {
4+
"nbextensions/vpython_libraries/jquery-ui.custom.min"], function(utils, Plotly) {
45

56
var comm
67
var ws = null
@@ -20,11 +21,30 @@ IPython.notebook.kernel.comm_manager.register_target('glow',
2021
comm.on_close(function(msg) {console.log("glow comm channel closed")});
2122

2223
if (msg.content.data.wsport !== undefined) {
23-
// create websocket instance
24-
var port = msg.content.data.wsport
25-
var uri = msg.content.data.wsuri
26-
ws = new WebSocket("ws://localhost:" + port + uri);
27-
ws.binaryType = "arraybuffer";
24+
// create websocket instance
25+
var port = msg.content.data.wsport
26+
var uri = msg.content.data.wsuri
27+
var loc = document.location, new_uri, url;
28+
29+
// Get base URL of current notebook server
30+
var base_url = utils.get_body_data('baseUrl');
31+
// Construct URL of our proxied service
32+
var service_url = base_url + 'proxy/' + port + uri;
33+
34+
if (loc.protocol === "https:") {
35+
new_uri = "wss:";
36+
} else {
37+
new_uri = "ws:";
38+
}
39+
if (document.location.hostname.includes("localhost")){
40+
url = "ws://localhost:" + port + uri;
41+
}
42+
else {
43+
new_uri += '//' + document.location.host + service_url;
44+
url = new_uri
45+
}
46+
ws = new WebSocket(url);
47+
ws.binaryType = "arraybuffer";
2848

2949
// Handle incoming websocket message callback
3050
ws.onmessage = function(evt) {
@@ -992,4 +1012,4 @@ function handle_attrs(dattrs) {
9921012
}
9931013
console.log("END OF GLOWCOMM")
9941014

995-
});
1015+
});

0 commit comments

Comments
 (0)