FINALLY MADE WEBSOCKETS WORK AAAAAAAAAAAAAAAAA
This commit is contained in:
+11
-6
@@ -1,5 +1,7 @@
|
||||
import sys
|
||||
import asyncio
|
||||
import threading
|
||||
import time
|
||||
|
||||
from macros import *
|
||||
import bot as botPy
|
||||
@@ -20,11 +22,14 @@ def main():
|
||||
py_print("Reading data...")
|
||||
data.read_data(bot)
|
||||
|
||||
if True: #bot.args["py"]:
|
||||
if not bot.args["py"]:
|
||||
py_print("Connecting to local websocket...")
|
||||
asyncio.run(py_websocket.websocket_client(bot))
|
||||
ws_thread = threading.Thread(target=py_websocket.run_thread, args=(bot,))
|
||||
ws_thread.start()
|
||||
|
||||
while not py_websocket.is_connected:
|
||||
py_print("Awaiting connection...")
|
||||
time.sleep(1)
|
||||
continue
|
||||
|
||||
#py_websocket.send_message("[Connection test] Hello from Python!")
|
||||
|
||||
|
||||
main()
|
||||
asyncio.run(py_websocket.send_message("[Connection test] Hello from Python!"))
|
||||
@@ -1,8 +1,11 @@
|
||||
import websockets
|
||||
import asyncio
|
||||
|
||||
from macros import py_print
|
||||
import bot as botPy
|
||||
|
||||
ws_global = None
|
||||
is_connected = False
|
||||
|
||||
async def send_message(message: str):
|
||||
global ws_global
|
||||
@@ -11,11 +14,18 @@ async def send_message(message: str):
|
||||
|
||||
|
||||
async def websocket_client(bot: botPy.Bot):
|
||||
global ws_global
|
||||
global ws_global, is_connected
|
||||
async with websockets.connect(f"ws://127.0.0.1:{bot.args["port"]}") as ws:
|
||||
ws_global = ws
|
||||
is_connected = True
|
||||
py_print(f"Connected webSocket server on ws://127.0.0.1:{bot.args["port"]}")
|
||||
|
||||
while True:
|
||||
response = await ws.recv()
|
||||
py_print(f"Received from Rust: {response}")
|
||||
py_print(f"Received from Rust: {response}")
|
||||
|
||||
|
||||
def run_thread(bot: botPy.Bot):
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
loop.run_until_complete(websocket_client(bot))
|
||||
Reference in New Issue
Block a user