DaniilAlpha commited on
Commit
8fc4ed2
·
1 Parent(s): 81a216e

Update accelerator.py

Browse files
Files changed (1) hide show
  1. accelerator.py +10 -6
accelerator.py CHANGED
@@ -1,16 +1,20 @@
1
  from typing import Union
2
- from fastapi import WebSocket
3
 
4
  class Accelerator:
5
- _connected = False
6
- def connected(self): return self._connected
7
-
8
  ws: Union[WebSocket, None]
9
 
 
 
10
  async def connect(self, ws: WebSocket):
11
  await ws.accept()
12
  self.ws = ws
13
 
14
  async def accelerate(self, input):
15
- await self.ws.send_text(input)
16
- return await self.ws.receive_text()
 
 
 
 
 
 
1
  from typing import Union
2
+ from fastapi.websockets import WebSocket, WebSocketState
3
 
4
  class Accelerator:
 
 
 
5
  ws: Union[WebSocket, None]
6
 
7
+ def connected(self): return self.ws != None
8
+
9
  async def connect(self, ws: WebSocket):
10
  await ws.accept()
11
  self.ws = ws
12
 
13
  async def accelerate(self, input):
14
+ while ws.client_state == WebSocketState.CONNECTING: pass
15
+
16
+ if ws.client_state == WebSocketState.CONNECTED:
17
+ await self.ws.send_text(input)
18
+ return await self.ws.receive_text()
19
+ elif ws.client_state == WebSocketState.DISCONNECTED:
20
+ ws = None