Open t1waz opened 3 months ago
As you suggested earlier, Firstly, the WebSocket handler class would need to be enhanced to include a close method. This method would be responsible for sending a closing handshake message to the client and terminating the connection.
Here's a simplified example:
class WebSocketHandler: def init(self, ws): self.ws = ws
def close(self, code=1000, reason=""):
# Send a closing handshake message to the client
self.ws.send_close(code, reason)
# Close the WebSocket connection
self.ws.close()
Then, in WebSocket view, we could use this close method to terminate the connection under specific conditions: As you suggested Example pseudo-code:
@websocket.on("message") def message(ws, msg) : if not is_valid(msg): ws.close() <----- implement this
Can you please provide reference to the code to contribute to it?
Description
Currently, Robyn framework lacks the capability to programmatically close a WebSocket connection from within the handler method. This feature is critical for implementing server-side logic that demands the closure of a WebSocket connection under specific conditions, such as the expiration of a user's authorization period.
Almost all major frameworks got this functionality
Use Case
A practical example of this feature would be in scenarios where a user's temporary access for WebSocket communication expires. For instance, if a user is only authorized to maintain a WebSocket connection for a certain period, the server should have the ability to terminate the connection from within the WebSocket view/handler once this period elapses. This is crucial for maintaining security and efficient resource management.
Suggested Implementation
The implementation could involve enhancing the existing WebSocket handler class/methods, allowing them to invoke a close connection method. This method should be able to send a closing handshake message to the client and terminate the connection gracefully.
Example pseudo-code: