Worked on the client
This commit is contained in:
parent
7156fbc378
commit
d9d3e46d44
43
client.py
43
client.py
@ -1,40 +1,41 @@
|
|||||||
from websockets import *
|
from websockets import *
|
||||||
import asyncio, json, sys
|
import asyncio, json, prompt_toolkit
|
||||||
|
|
||||||
class TestClient:
|
class TestClient:
|
||||||
def __init__(self) -> None:
|
def __init__(self, address: str = "127.0.0.1", port: int = 8765) -> None:
|
||||||
self.connection: ClientConnection = None
|
self.connection: ClientConnection = None
|
||||||
|
self.msgId: int = 0
|
||||||
|
self.connected = True
|
||||||
|
asyncio.run(self.main(address, port))
|
||||||
|
|
||||||
def main(self) -> None:
|
async def main(self, address: str, port: int) -> None:
|
||||||
asyncio.run(self.connect())
|
await self.connect(address, port)
|
||||||
asyncio.run(self.receive())
|
tasks: list[asyncio.Task] = []
|
||||||
|
async with asyncio.TaskGroup() as tg:
|
||||||
|
tasks.append(tg.create_task(self.__message__()))
|
||||||
|
tasks.append(tg.create_task(self.receive()))
|
||||||
|
print("Finished")
|
||||||
|
await self.connection.close()
|
||||||
|
|
||||||
async def connect(self, address: str = "127.0.0.1", port: int = 8765) -> None:
|
async def connect(self, address: str = "127.0.0.1", port: int = 8765) -> None:
|
||||||
uri: str = f"ws://{address}:{port}"
|
uri: str = f"ws://{address}:{port}"
|
||||||
async with connect(uri) as ws:
|
self.connection = await connect(uri)
|
||||||
self.connection = ws
|
|
||||||
loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
|
|
||||||
loop.add_reader(0, self.message)
|
|
||||||
await self.connection.wait_closed()
|
|
||||||
|
|
||||||
async def disconnect(self):
|
async def __message__(self) -> None:
|
||||||
await self.connection.close()
|
session = prompt_toolkit.PromptSession()
|
||||||
self.connection = None
|
while (self.connected):
|
||||||
|
text: str = await session.prompt_async()
|
||||||
def message(self) -> None:
|
print(f"Text: '{text}'")
|
||||||
text = sys.stdin.readline()
|
diction: dict = {"ID": self.msgId, "message": text}
|
||||||
diction: dict = {"message": text}
|
|
||||||
msg = json.dumps(diction)
|
msg = json.dumps(diction)
|
||||||
asyncio.run(self.__message__(msg))
|
|
||||||
|
|
||||||
async def __message__(self, msg) -> None:
|
|
||||||
await self.connection.send(msg)
|
await self.connection.send(msg)
|
||||||
|
self.msgId += 1
|
||||||
print(f"Client sent {msg}")
|
print(f"Client sent {msg}")
|
||||||
|
|
||||||
async def receive(self) -> None:
|
async def receive(self) -> None:
|
||||||
|
while (self.connected):
|
||||||
response: str = await self.connection.recv()
|
response: str = await self.connection.recv()
|
||||||
print(f"Received: {response}")
|
print(f"Received: {response}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
client = TestClient()
|
client = TestClient()
|
||||||
client.main()
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
MarkupSafe==3.0.2
|
MarkupSafe==3.0.2
|
||||||
|
prompt_toolkit==3.0.52
|
||||||
|
wcwidth==0.2.14
|
||||||
websockets==15.0.1
|
websockets==15.0.1
|
||||||
Werkzeug==3.1.3
|
Werkzeug==3.1.3
|
||||||
|
@ -21,8 +21,9 @@ class WebSocketServer:
|
|||||||
while (connected):
|
while (connected):
|
||||||
raw: str = await connection.recv()
|
raw: str = await connection.recv()
|
||||||
message = json.loads(raw)
|
message = json.loads(raw)
|
||||||
print(f"Received: {message}")
|
print(f"Received: {message["message"]} width id {message["ID"]}")
|
||||||
await connection.send('{"message":"received"}')
|
response = {"ID": message["ID"], "message": f"received: {message["message"]}"}
|
||||||
|
await connection.send(json.dumps(response))
|
||||||
print("Server replied")
|
print("Server replied")
|
||||||
|
|
||||||
def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this
|
def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this
|
||||||
|
Loading…
x
Reference in New Issue
Block a user