Skip to content

Commit 98e9389

Browse files
diningPhilosopher64Prabhakar Kumar
authored andcommitted
Bug fixes with handling Websockets
Closes #9
1 parent e667eac commit 98e9389

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

matlab_proxy/app.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,11 @@ async def matlab_view(req):
375375

376376
# WebSocket
377377
if (
378-
reqH.get("connection") == "Upgrade"
379-
and reqH.get("upgrade") == "websocket"
378+
reqH.get("connection")
379+
# Fixes issue #9 on github.
380+
and reqH.get("connection").lower() == "upgrade"
381+
and reqH.get("upgrade")
382+
and reqH.get("upgrade").lower() == "websocket"
380383
and req.method == "GET"
381384
):
382385
ws_server = web.WebSocketResponse()
@@ -394,6 +397,19 @@ async def wsforward(ws_from, ws_to):
394397
async for msg in ws_from:
395398
mt = msg.type
396399
md = msg.data
400+
401+
# When a websocket is closed by the MATLAB JSD, it sends out a few http requests to the Embedded Connector about the events
402+
# that had occured (figureWindowClosed etc.)
403+
# The Embedded Connector responds by sending a message of type 'Error' with close code as Abnormal closure.
404+
# When this happens, matlab-proxy can safely exit out of the loop
405+
# and close the websocket connection it has with the Embedded Connector (ws_client)
406+
if (
407+
mt == aiohttp.WSMsgType.ERROR
408+
and ws_from.close_code
409+
== aiohttp.WSCloseCode.ABNORMAL_CLOSURE
410+
):
411+
break
412+
397413
if mt == aiohttp.WSMsgType.TEXT:
398414
await ws_to.send_str(md)
399415
elif mt == aiohttp.WSMsgType.BINARY:

0 commit comments

Comments
 (0)