Skip to content

Commit c49a198

Browse files
tan01copybara-github
authored andcommitted
Remove sockJS and use websocket for the front-end.
This attempts to remove both tornado-sockjs from the HTF servers and sockJS from the front-end getting served. SockJS is a websocket emulator used as a compatibility shim in environments that don't support the websocket API. However, the TornadoSockJS library is no longer being maintained. PiperOrigin-RevId: 647392280
1 parent 6060cd0 commit c49a198

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

openhtf/output/servers/dashboard_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from openhtf.output.web_gui import web_launcher
3333
from openhtf.util import data
3434
from openhtf.util import multicast
35-
import sockjs.tornado
3635
import tornado.web
3736

3837
_LOG = logging.getLogger(__name__)
@@ -128,10 +127,11 @@ class DashboardServer(web_gui_server.WebGuiServer):
128127
"""Serves a list of known stations and an Angular frontend."""
129128

130129
def __init__(self, port):
131-
dash_router = sockjs.tornado.SockJSRouter(DashboardPubSub, '/sub/dashboard')
132-
routes = dash_router.urls + [
130+
routes = [
133131
('/station_list', StationListHandler),
132+
('/sub/dashboard', DashboardPubSub),
134133
]
134+
_LOG.info('Initializing dashboard server.')
135135
super(DashboardServer, self).__init__(routes, port)
136136

137137
def _get_config(self):

openhtf/output/servers/pub_sub.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import logging
1717

1818
from openhtf import util as htf_util
19-
import sockjs.tornado
19+
import tornado.websocket
2020

2121
_LOG = logging.getLogger(__name__)
2222

2323

24-
class PubSub(sockjs.tornado.SockJSConnection):
24+
class PubSub(tornado.websocket.WebSocketHandler):
2525
"""Generic pub/sub based on SockJS connections."""
2626

2727
@htf_util.classproperty
@@ -53,22 +53,28 @@ def publish(cls, message, client_filter=None):
5353
if (not client_filter) or client_filter(client):
5454
client.send(message)
5555

56-
def on_open(self, info):
57-
_LOG.debug('New subscriber from %s.', info.ip)
56+
def open(self, *args, **kwargs):
5857
with self._lock: # pylint: disable=not-context-manager
5958
self.subscribers.add(self)
60-
self.on_subscribe(info)
59+
self.on_subscribe(None)
6160

6261
def on_close(self):
63-
_LOG.debug('A client unsubscribed.')
62+
_LOG.info('A client unsubscribed.')
6463
with self._lock: # pylint: disable=not-context-manager
6564
self.subscribers.remove(self)
6665
self.on_unsubscribe()
6766

67+
def send(self, message):
68+
self.write_message(message)
69+
6870
def on_subscribe(self, info):
6971
"""Called when new clients subscribe. Subclasses can override."""
7072
pass
7173

7274
def on_unsubscribe(self):
7375
"""Called when clients unsubscribe. Subclasses can override."""
7476
pass
77+
78+
def check_origin(self, origin: str) -> bool:
79+
"""Allows all cross-origin traffic. Dangerous."""
80+
return True

openhtf/output/servers/station_server.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from openhtf.util import functions
4040
from openhtf.util import multicast
4141
from openhtf.util import timeouts
42-
import sockjs.tornado
42+
import tornado
4343

4444
CONF = configuration.CONF
4545

@@ -225,7 +225,7 @@ def _to_dict_with_event(cls, test_state):
225225
return test_state_dict, event
226226

227227

228-
class DashboardPubSub(sockjs.tornado.SockJSConnection):
228+
class DashboardPubSub(tornado.websocket.WebSocketHandler):
229229
"""WebSocket endpoint for the list of available stations.
230230
231231
In this case, there is always exactly one available station: the station
@@ -244,9 +244,9 @@ def for_port(cls, port):
244244
"""Returns a new subclass with the port set."""
245245
return type(cls.__name__, (cls,), {'port': port})
246246

247-
def on_open(self, unused_info):
247+
def open(self, unused_info):
248248
"""Called by the base class when a client connects."""
249-
self.send(self._make_message())
249+
self.write_message(self._make_message())
250250

251251
@classmethod
252252
def _make_message(cls):
@@ -595,11 +595,12 @@ def __init__(
595595
station_watcher = StationWatcher(StationPubSub.publish_update)
596596
station_watcher.start()
597597

598-
# Set up the SockJS endpoints.
598+
# Set up the endpoints.
599599
dashboard_class = DashboardPubSub.for_port(port)
600-
dash_router = sockjs.tornado.SockJSRouter(dashboard_class, '/sub/dashboard')
601-
station_router = sockjs.tornado.SockJSRouter(StationPubSub, '/sub/station')
602-
routes = dash_router.urls + station_router.urls
600+
routes = [
601+
('/sub/dashboard', dashboard_class),
602+
('/sub/station', StationPubSub),
603+
]
603604

604605
# Set up the other endpoints.
605606
routes.extend((

0 commit comments

Comments
 (0)