Arguments in wssreceiver, 1.0.5

This commit is contained in:
DarkCat09 2022-05-18 19:35:46 +04:00
parent b02e833702
commit cc3099123e
4 changed files with 47 additions and 7 deletions

View file

@ -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())

View file

@ -16,5 +16,10 @@ async def console(msg):
async def main(): async def main():
s.start() s.start()
await socket.connect() await socket.connect()
await asyncio.create_task(loop())
async def loop():
while True:
await asyncio.sleep(1)
asyncio.run(main()) asyncio.run(main())

View file

@ -3,7 +3,7 @@ import json
import asyncio import asyncio
import logging import logging
import websockets 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 typing import TYPE_CHECKING
from .atconnect import REQUA from .atconnect import REQUA
@ -38,9 +38,9 @@ class AternosWss:
self.atserv.confirm() 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: def decorator(func:Callable[[Any],Coroutine[Any,Any,None]]) -> None:
self.recv[stream] = func self.recv[stream] = (func, args)
return decorator return decorator
async def connect(self) -> None: async def connect(self) -> None:
@ -141,9 +141,19 @@ class AternosWss:
msg = json.loads(obj['message']) msg = json.loads(obj['message'])
if msgtype in self.recv: 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: except asyncio.CancelledError:
pass pass

View file

@ -5,7 +5,7 @@ with open('README.md', 'rt') as readme:
setuptools.setup( setuptools.setup(
name='python-aternos', name='python-aternos',
version='1.0.4', version='1.0.5',
author='Chechkenev Andrey (@DarkCat09)', author='Chechkenev Andrey (@DarkCat09)',
author_email='aacd0709@mail.ru', author_email='aacd0709@mail.ru',
description='An unofficial Aternos API', description='An unofficial Aternos API',