Arguments in wssreceiver, 1.0.5
This commit is contained in:
parent
b02e833702
commit
cc3099123e
4 changed files with 47 additions and 7 deletions
25
examples/websocket_args_example.py
Normal file
25
examples/websocket_args_example.py
Normal 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())
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
2
setup.py
2
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',
|
||||
|
|
Reference in a new issue