Skip to content

Commit ec99491

Browse files
authored
Merge pull request #29 from jcoady/master
Add websocket support through proxied server
2 parents e9e2660 + 88212d5 commit ec99491

File tree

6 files changed

+63
-23
lines changed

6 files changed

+63
-23
lines changed

labextension/vpython/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vpython",
3-
"version": "0.4.0",
3+
"version": "0.4.3",
44
"description": "VPython extension for Jupyterlab",
55
"keywords": [
66
"jupyter",
@@ -27,10 +27,11 @@
2727
"jpinstall": "jupyter labextension install ."
2828
},
2929
"dependencies": {
30-
"@jupyterlab/application": "^1.1.2",
31-
"@jupyterlab/apputils": "^1.1.2",
32-
"@jupyterlab/docregistry": "^1.1.2",
33-
"@jupyterlab/notebook": "^1.1.2",
30+
"@jupyterlab/application": "^1.1.3",
31+
"@jupyterlab/apputils": "^1.1.3",
32+
"@jupyterlab/coreutils": "^3.1.0",
33+
"@jupyterlab/docregistry": "^1.1.3",
34+
"@jupyterlab/notebook": "^1.1.3",
3435
"@phosphor/disposable": "^1.3.0",
3536
"script-loader": "^0.7.2"
3637
},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function onmessage (msg: any): void;
2-
export function setupWebsocket (msg: any): void;
2+
export function setupWebsocket (msg: any, serviceUrl: any): void;
33
export var comm: any;

labextension/vpython/src/glowcommlab.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,31 @@ var isopen = false
1515

1616
console.log('START OF GLOWCOMM')
1717

18-
export function createWebsocket(msg) {
18+
export function createWebsocket(msg, serviceUrl) {
1919
if (msg.content.data.wsport !== undefined) {
2020
// create websocket instance
21-
var port = msg.content.data.wsport
22-
var uri = msg.content.data.wsuri
23-
ws = new WebSocket("ws://localhost:" + port + uri);
24-
ws.binaryType = "arraybuffer";
21+
var port = msg.content.data.wsport
22+
var uri = msg.content.data.wsuri
23+
var loc = document.location, new_uri, url;
24+
25+
if (loc.protocol === "https:") {
26+
new_uri = "wss:";
27+
} else {
28+
new_uri = "ws:";
29+
}
30+
if (document.location.hostname.includes("localhost")){
31+
url = "ws://localhost:" + port + uri;
32+
}
33+
else {
34+
new_uri += '//' + document.location.host + service_url + uri;
35+
url = new_uri
36+
}
37+
ws = new WebSocket(url);
38+
ws.binaryType = "arraybuffer";
2539

2640
// Handle incoming websocket message callback
2741
ws.onmessage = function(evt) {
28-
console.log("WebSocket Message Received: " + evt.data)
42+
console.log("WebSocket Message Received: " + evt.data)
2943
};
3044

3145
// Close Websocket callback

labextension/vpython/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
NotebookPanel, INotebookModel
1515
} from '@jupyterlab/notebook';
1616

17-
17+
import { PageConfig } from '@jupyterlab/coreutils';
1818

1919
/**
2020
* The plugin registration information.
@@ -47,8 +47,13 @@ class VPythonExtension implements DocumentRegistry.IWidgetExtension<NotebookPane
4747
glowcommlab.comm = vp_comm
4848
vp_comm.onMsg = glowcommlab.onmessage
4949

50-
glowcommlab.setupWebsocket(commMsg)
50+
// Get base URL of current notebook server
51+
let baseUrl = PageConfig.getBaseUrl()
52+
53+
// Construct URL of our proxied service
54+
let serviceUrl = base_url + 'proxy/' + port;
5155

56+
glowcommlab.setupWebsocket(commMsg, serviceUrl)
5257
});
5358

5459
vp_comm.onClose = (msg) => {console.log("comm onClose");};

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Cython
2-
vpnotebook
32
vpython
3+
jupyter-server-proxy

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)