Fixed handlers overwriting (#55), improved WS API
This commit is contained in:
parent
70fd2239e0
commit
bb019d1416
3 changed files with 150 additions and 30 deletions
|
@ -1,34 +1,47 @@
|
|||
import asyncio
|
||||
import logging
|
||||
from getpass import getpass
|
||||
from python_aternos import Client, atwss
|
||||
|
||||
from typing import Tuple, Dict, Any
|
||||
|
||||
from python_aternos import Client, Streams
|
||||
|
||||
# Request credentials
|
||||
user = input('Username: ')
|
||||
pswd = getpass('Password: ')
|
||||
|
||||
# Debug logging
|
||||
logs = input('Show detailed logs? (y/n) ').strip().lower() == 'y'
|
||||
if logs:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
# Authentication
|
||||
aternos = Client.from_credentials(user, pswd)
|
||||
|
||||
s = aternos.list_servers()[0]
|
||||
socket = s.wss()
|
||||
server = aternos.list_servers()[0]
|
||||
socket = server.wss()
|
||||
|
||||
|
||||
@socket.wssreceiver(atwss.Streams.console, 'Server 1')
|
||||
async def console(msg, args):
|
||||
# Handler for console messages
|
||||
@socket.wssreceiver(Streams.console, ('Server 1',))
|
||||
async def console(
|
||||
msg: Dict[Any, Any],
|
||||
args: Tuple[str]) -> None:
|
||||
|
||||
print(args[0], 'received', msg)
|
||||
|
||||
|
||||
async def main():
|
||||
s.start()
|
||||
# Main function
|
||||
async def main() -> None:
|
||||
server.start()
|
||||
await socket.connect()
|
||||
await asyncio.create_task(loop())
|
||||
|
||||
|
||||
async def loop():
|
||||
# Keepalive
|
||||
async def loop() -> None:
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
await asyncio.Future()
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
|
|
55
examples/websocket_status_example.py
Normal file
55
examples/websocket_status_example.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
import asyncio
|
||||
import logging
|
||||
from getpass import getpass
|
||||
|
||||
from typing import Tuple, Dict, Any
|
||||
|
||||
from python_aternos import Client, Streams
|
||||
|
||||
# Request credentials
|
||||
user = input('Username: ')
|
||||
pswd = getpass('Password: ')
|
||||
|
||||
# Debug logging
|
||||
logs = input('Show detailed logs? (y/n) ').strip().lower() == 'y'
|
||||
if logs:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
# Authentication
|
||||
aternos = Client.from_credentials(user, pswd)
|
||||
|
||||
server = aternos.list_servers()[0]
|
||||
socket = server.wss()
|
||||
|
||||
|
||||
# Handler for server status
|
||||
@socket.wssreceiver(Streams.status, ('Server 1',))
|
||||
async def state(
|
||||
msg: Dict[Any, Any],
|
||||
args: Tuple[str]) -> None:
|
||||
|
||||
print(args[0], 'received', len(msg), 'symbols')
|
||||
|
||||
server._info = msg
|
||||
print(
|
||||
args[0],
|
||||
server.subdomain,
|
||||
'is',
|
||||
server.status
|
||||
)
|
||||
|
||||
|
||||
# Main function
|
||||
async def main() -> None:
|
||||
server.start()
|
||||
await socket.connect()
|
||||
await asyncio.create_task(loop())
|
||||
|
||||
|
||||
# Keepalive
|
||||
async def loop() -> None:
|
||||
while True:
|
||||
await asyncio.Future()
|
||||
|
||||
|
||||
asyncio.run(main())
|
Reference in a new issue