-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
# Copyright (c) 2024 iiPython | ||
|
||
# Modules | ||
from typing import Any | ||
|
||
import orjson | ||
from websockets import WebSocketCommonProtocol | ||
|
||
class NightwatchClient(): | ||
"""This class acts as a wrapper on top of WebSocketCommonProtocol that implements | ||
data serialization through orjson.""" | ||
def __init__(self, client: WebSocketCommonProtocol) -> None: | ||
def __init__(self, state, client: WebSocketCommonProtocol) -> None: | ||
self.client = client | ||
self.identified = False | ||
|
||
self.state = state | ||
self.state.add_client(client) | ||
|
||
async def send(self, message_type: str, **message_data) -> None: | ||
await self.client.send(orjson.dumps({"type": message_type, "data": message_data})) | ||
|
||
# Handle user data (ie. name and color) | ||
def set_user_data(self, data: dict[str, Any]) -> None: | ||
self.user_data = data | ||
self.state.clients[self.client] = data["name"] |