Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit dd72d02

Browse files
committed
WebSockets bugfix, updated Readme
1 parent b1f40db commit dd72d02

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ if testserv != None:
6262
|v0.3|Implemented files API, added typization.|
6363
|v0.4|Implemented configuration API, some bugfixes.|
6464
|v0.5|The API was updated corresponding to new Aternos security methods. Huge thanks to [lusm554](https://github.com/lusm554).|
65-
|v0.6|Code refactoring, unit-tests, websocket API and session saving to prevent detecting automation access.|
66-
|v0.7|Full implementation of config API and Google Drive backups is planned.|
67-
|v0.8|Shared access API and permission management is planned.|
65+
|v0.6|Code refactoring, websockets API and session saving to prevent detecting automation access.|
66+
|v0.7|Full implementation of config and software API, unit tests and documentation is planned.|
67+
|v0.8|Shared access API and Google Drive backups is planned.|
6868
|v0.9.x|A long debugging before stable release, SemVer version code.|
6969

7070
## License

examples/websocket_example.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from getpass import getpass
23
from python_aternos import Client, atwss
34

@@ -10,6 +11,10 @@
1011

1112
@socket.wssreceiver(atwss.Streams.console)
1213
async def console(msg):
13-
print('Received: ' + msg)
14+
print('Received:', msg)
1415

15-
s.start()
16+
async def main():
17+
s.start()
18+
await socket.connect()
19+
20+
asyncio.run(main())

python_aternos/atserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Edition(enum.IntEnum):
1717
class Status(enum.IntEnum):
1818
off = 0
1919
on = 1
20-
loading = 2
20+
starting = 2
2121
shutdown = 3
2222
unknown = 6
2323
error = 7

python_aternos/atwss.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import enum
22
import json
33
import asyncio
4+
import logging
45
import websockets
56
from typing import Union, Any, Dict, Callable, Coroutine
67
from typing import TYPE_CHECKING
@@ -9,12 +10,17 @@
910
if TYPE_CHECKING:
1011
from .atserver import AternosServer
1112

12-
class Streams(enum.IntEnum):
13-
status = 0
14-
queue = 1
15-
console = 2
16-
ram = 3
17-
tps = 4
13+
class Streams(enum.Enum):
14+
15+
status = (0,None)
16+
queue = (1,None)
17+
console = (2,'console')
18+
ram = (3,'heap')
19+
tps = (4,'tick')
20+
21+
def __init__(self, num:int, stream:str):
22+
self.num = num
23+
self.stream = stream
1824

1925
class AternosWss:
2026

@@ -23,7 +29,7 @@ def __init__(self, atserv:'AternosServer', autoconfirm:bool=False) -> None:
2329
self.atserv = atserv
2430
self.cookies = atserv.atconn.session.cookies
2531
self.session = self.cookies['ATERNOS_SESSION']
26-
self.servid = self.cookies['ATERNOS_SERVER']
32+
self.servid = atserv.servid
2733
self.recv = {}
2834
self.autoconfirm = autoconfirm
2935
self.confirmed = False
@@ -32,7 +38,7 @@ async def confirm(self) -> None:
3238

3339
self.atserv.confirm()
3440

35-
def wssreceiver(self, stream:int) -> Callable[[Callable[[Any],Coroutine[Any,Any,None]]],Any]:
41+
def wssreceiver(self, stream:Streams) -> Callable[[Callable[[Any],Coroutine[Any,Any,None]]],Any]:
3642
def decorator(func:Callable[[Any],Coroutine[Any,Any,None]]) -> None:
3743
self.recv[stream] = func
3844
return decorator
@@ -53,6 +59,30 @@ async def connect(self) -> None:
5359
origin='https://aternos.org',
5460
extra_headers=headers
5561
)
62+
63+
@self.wssreceiver(Streams.status)
64+
async def confirmfunc(msg):
65+
# Autoconfirm
66+
if not self.autoconfirm:
67+
return
68+
if msg['class'] == 'queueing' \
69+
and msg['queue']['pending'] == 'pending'\
70+
and not self.confirmed:
71+
self.confirm()
72+
73+
@self.wssreceiver(Streams.status)
74+
async def streamsfunc(msg):
75+
if msg['status'] == 2:
76+
# Automatically start streams
77+
for strm in self.recv:
78+
if not isinstance(strm,Streams):
79+
continue
80+
if strm.stream:
81+
logging.debug(f'Enabling {strm.stream} stream')
82+
await self.send({
83+
'stream': strm.stream,
84+
'type': 'start'
85+
})
5686

5787
await self.wssworker()
5888

@@ -66,7 +96,7 @@ async def send(self, obj:Union[Dict[str, Any],str]) -> None:
6696
if isinstance(obj, dict):
6797
obj = json.dumps(obj)
6898

69-
self.socket.send(obj)
99+
await self.socket.send(obj)
70100

71101
async def wssworker(self) -> None:
72102

@@ -86,6 +116,7 @@ async def receiver(self) -> None:
86116
while True:
87117
data = await self.socket.recv()
88118
obj = json.loads(data)
119+
msgtype = -1
89120

90121
if obj['type'] == 'line':
91122
msgtype = Streams.console
@@ -104,16 +135,6 @@ async def receiver(self) -> None:
104135
msgtype = Streams.status
105136
msg = json.loads(obj['message'])
106137

107-
if not self.autoconfirm:
108-
continue
109-
if msg['class'] == 'queueing' \
110-
and msg['queue']['pending'] == 'pending'\
111-
and not self.confirmed:
112-
t = asyncio.create_task(
113-
self.confirm()
114-
)
115-
await t
116-
117138
if msgtype in self.recv:
118139
t = asyncio.create_task(
119140
self.recv[msgtype](msg)

0 commit comments

Comments
 (0)