diff --git a/examples/websocket_args_example.py b/examples/websocket_args_example.py new file mode 100644 index 0000000..60abfb5 --- /dev/null +++ b/examples/websocket_args_example.py @@ -0,0 +1,25 @@ +import asyncio +from getpass import getpass +from python_aternos import Client, atwss + +user = input('Username: ') +pswd = getpass('Password: ') +aternos = Client.from_credentials(user, pswd) + +s = aternos.list_servers()[0] +socket = s.wss() + +@socket.wssreceiver(atwss.Streams.console, 'Server 1') +async def console(msg, args): + print(args[0], 'received', msg) + +async def main(): + s.start() + await socket.connect() + await asyncio.create_task(loop()) + +async def loop(): + while True: + await asyncio.sleep(1) + +asyncio.run(main()) diff --git a/examples/websocket_example.py b/examples/websocket_example.py index cb32523..eb5714b 100644 --- a/examples/websocket_example.py +++ b/examples/websocket_example.py @@ -16,5 +16,10 @@ async def console(msg): async def main(): s.start() await socket.connect() + await asyncio.create_task(loop()) + +async def loop(): + while True: + await asyncio.sleep(1) asyncio.run(main()) diff --git a/python_aternos/atwss.py b/python_aternos/atwss.py index 5c8636b..9827aa3 100644 --- a/python_aternos/atwss.py +++ b/python_aternos/atwss.py @@ -3,7 +3,7 @@ import json import asyncio import logging import websockets -from typing import Union, Any, Dict, Callable, Coroutine +from typing import Union, Any, Dict, Callable, Coroutine, Tuple from typing import TYPE_CHECKING from .atconnect import REQUA @@ -38,9 +38,9 @@ class AternosWss: self.atserv.confirm() - def wssreceiver(self, stream:Streams) -> Callable[[Callable[[Any],Coroutine[Any,Any,None]]],Any]: + def wssreceiver(self, stream:Streams, *args:Any) -> Callable[[Callable[[Any],Coroutine[Any,Any,None]]],Any]: def decorator(func:Callable[[Any],Coroutine[Any,Any,None]]) -> None: - self.recv[stream] = func + self.recv[stream] = (func, args) return decorator async def connect(self) -> None: @@ -141,9 +141,19 @@ class AternosWss: msg = json.loads(obj['message']) if msgtype in self.recv: - await asyncio.create_task( - self.recv[msgtype](msg) - ) + + # function info tuple: + # (function, arguments) + func = self.recv[msgtype] + + # if arguments is not empty + if func[1]: + # call the function with args + coro = func[0](msg, func[1]) + else: + coro = func[0](msg) + # run + await asyncio.create_task(coro) except asyncio.CancelledError: pass diff --git a/setup.py b/setup.py index 4106bc9..d7b4bbd 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'rt') as readme: setuptools.setup( name='python-aternos', - version='1.0.4', + version='1.0.5', author='Chechkenev Andrey (@DarkCat09)', author_email='aacd0709@mail.ru', description='An unofficial Aternos API',