Skip to content

Commit ed4b8fe

Browse files
author
Joel Collins
committed
Rearranged socket handler
1 parent 41fae69 commit ed4b8fe

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from ..sockets import SocketSubscriber, socket_handler_loop
1+
from ..sockets import SocketSubscriber
22
from ..find import current_labthing
33

4+
import gevent
45
import logging
56

67

@@ -10,7 +11,21 @@ def socket_handler(ws):
1011
current_labthing().subscribers.add(wssub)
1112
logging.info(f"Added subscriber {wssub}")
1213
# Start the socket connection handler loop
13-
socket_handler_loop(ws)
14+
while not ws.closed:
15+
message = ws.receive()
16+
if message is None:
17+
break
18+
response = process_socket_message(message)
19+
if response:
20+
ws.send(response)
21+
gevent.sleep(0.1)
1422
# Remove the subscriber once the loop returns
1523
current_labthing().subscribers.remove(wssub)
1624
logging.info(f"Removed subscriber {wssub}")
25+
26+
27+
def process_socket_message(message: str):
28+
if message:
29+
return None
30+
else:
31+
return None
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .gevent import Sockets, SocketSubscriber, socket_handler_loop
1+
from .gevent import Sockets, SocketSubscriber

src/labthings/server/sockets/gevent.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,3 @@ def wsgi_app(self, environ, start_response):
181181
return []
182182
except (NotFound, KeyError):
183183
return self.app_wsgi_app(environ, start_response)
184-
185-
186-
def socket_handler_loop(ws):
187-
while not ws.closed:
188-
message = ws.receive()
189-
if message is None:
190-
break
191-
response = process_socket_message(message)
192-
if response:
193-
ws.send(response)
194-
gevent.sleep(0.1)
195-
196-
197-
def process_socket_message(message: str):
198-
if message:
199-
return None
200-
else:
201-
return None

0 commit comments

Comments
 (0)