Skip to content

Commit 3a784f0

Browse files
committed
Added an option to change the host NetworkTables uses to connect to the websocket. This is useful when the server serving the static files is different from the server serving the NetworkTables script and websocket.
1 parent 2be2c9a commit 3a784f0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pynetworktables2js/js/networktables.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,17 @@ var NetworkTables = new function () {
294294
host = "ws:";
295295
}
296296

297-
host += "//" + loc.host;
297+
// If the websocket is being served from a different host allow users
298+
// to add a data-nt-host="" attribute to the script tag loading
299+
// Networktables.
300+
var ntHostElement = document.querySelector('[data-nt-host]');
301+
if (ntHostElement) {
302+
var ntHost = ntHostElement.getAttribute('data-nt-host');
303+
host += "//" + ntHost;
304+
} else {
305+
host += "//" + loc.host;
306+
}
307+
298308
host += "/networktables/ws";
299309

300310
function createSocket() {

pynetworktables2js/tornado_handlers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def open(self):
2626
self.ioloop = IOLoop.current()
2727
self.ntserial = NTSerial(self.send_msg_threadsafe)
2828

29+
def check_origin(self, origin):
30+
'''Allow CORS requests'''
31+
return True
32+
2933
def on_message(self, message):
3034
if self.ntserial is not None:
3135
self.ntserial.process_update(message)

0 commit comments

Comments
 (0)