2018-09-18 12:19:45 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-12-11 12:30:31 +01:00
|
|
|
"""websocket cmd client for web_ws.py example."""
|
|
|
|
|
2018-09-18 12:19:45 +02:00
|
|
|
import argparse
|
|
|
|
import asyncio
|
|
|
|
import sys
|
2022-12-11 12:30:31 +01:00
|
|
|
from contextlib import suppress
|
2018-09-18 12:19:45 +02:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
|
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
async def start_client(url: str) -> None:
|
|
|
|
name = input("Please enter your name: ")
|
2018-09-18 12:19:45 +02:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
async def dispatch(ws: aiohttp.ClientWebSocketResponse) -> None:
|
2018-12-31 07:28:42 +01:00
|
|
|
while True:
|
|
|
|
msg = await ws.receive()
|
2022-12-11 12:30:31 +01:00
|
|
|
|
2018-09-18 12:19:45 +02:00
|
|
|
if msg.type == aiohttp.WSMsgType.TEXT:
|
2022-12-11 12:30:31 +01:00
|
|
|
print("Text: ", msg.data.strip())
|
2018-09-18 12:19:45 +02:00
|
|
|
elif msg.type == aiohttp.WSMsgType.BINARY:
|
2022-12-11 12:30:31 +01:00
|
|
|
print("Binary: ", msg.data)
|
2018-09-18 12:19:45 +02:00
|
|
|
elif msg.type == aiohttp.WSMsgType.PING:
|
2018-12-31 07:28:42 +01:00
|
|
|
await ws.pong()
|
2018-09-18 12:19:45 +02:00
|
|
|
elif msg.type == aiohttp.WSMsgType.PONG:
|
2022-12-11 12:30:31 +01:00
|
|
|
print("Pong received")
|
2018-09-18 12:19:45 +02:00
|
|
|
else:
|
|
|
|
if msg.type == aiohttp.WSMsgType.CLOSE:
|
2018-12-31 07:28:42 +01:00
|
|
|
await ws.close()
|
2018-09-18 12:19:45 +02:00
|
|
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
2022-12-11 12:30:31 +01:00
|
|
|
print("Error during receive %s" % ws.exception())
|
2018-09-18 12:19:45 +02:00
|
|
|
elif msg.type == aiohttp.WSMsgType.CLOSED:
|
|
|
|
pass
|
2018-12-31 07:28:42 +01:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
break
|
2018-12-31 07:28:42 +01:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.ws_connect(url, autoclose=False, autoping=False) as ws:
|
|
|
|
# send request
|
|
|
|
dispatch_task = asyncio.create_task(dispatch(ws))
|
2018-12-31 07:28:42 +01:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
# Exit with Ctrl+D
|
|
|
|
while line := await asyncio.to_thread(sys.stdin.readline):
|
2023-09-10 21:22:35 +02:00
|
|
|
if line.startswith("/"):
|
|
|
|
await ws.send_str(line)
|
|
|
|
else:
|
|
|
|
await ws.send_str(name + ": " + line)
|
2018-12-31 07:28:42 +01:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
dispatch_task.cancel()
|
|
|
|
with suppress(asyncio.CancelledError):
|
|
|
|
await dispatch_task
|
2018-09-18 12:19:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
ARGS = argparse.ArgumentParser(
|
2022-12-11 12:30:31 +01:00
|
|
|
description="websocket console client for wssrv.py example."
|
|
|
|
)
|
2018-09-18 12:19:45 +02:00
|
|
|
ARGS.add_argument(
|
2022-12-11 12:30:31 +01:00
|
|
|
"--host", action="store", dest="host", default="127.0.0.1", help="Host name"
|
|
|
|
)
|
2018-09-18 12:19:45 +02:00
|
|
|
ARGS.add_argument(
|
2022-12-11 12:30:31 +01:00
|
|
|
"--port", action="store", dest="port", default=8080, type=int, help="Port number"
|
|
|
|
)
|
2018-09-18 12:19:45 +02:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
if __name__ == "__main__":
|
2018-09-18 12:19:45 +02:00
|
|
|
args = ARGS.parse_args()
|
2022-12-11 12:30:31 +01:00
|
|
|
if ":" in args.host:
|
|
|
|
args.host, port = args.host.split(":", 1)
|
2018-09-18 12:19:45 +02:00
|
|
|
args.port = int(port)
|
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
url = f"http://{args.host}:{args.port}/ws"
|
2018-09-18 12:19:45 +02:00
|
|
|
|
2022-12-11 12:30:31 +01:00
|
|
|
print("""
|
|
|
|
/list list all available rooms
|
|
|
|
/join name join room, if room does not exist, create new one
|
|
|
|
/name name set session name
|
|
|
|
some message just string, send message to all peers in same room
|
|
|
|
ctrl-D to exit
|
|
|
|
""")
|
|
|
|
asyncio.run(start_client(url))
|