FINALLY MADE WEBSOCKETS WORK AAAAAAAAAAAAAAAAA
This commit is contained in:
@@ -1,13 +0,0 @@
|
|||||||
--- Run command ---
|
|
||||||
cargo run [-- {options}]
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
cargo run
|
|
||||||
cargo run -- --dev
|
|
||||||
cargo run -- --py --dev
|
|
||||||
|
|
||||||
--- Options: ---
|
|
||||||
--h, --help - View this text.
|
|
||||||
--dev - Run in dev mode. Disables certain security measures.
|
|
||||||
--rs - Only run the Rust portion of the program.
|
|
||||||
--py - Only run the Python portion of the program.
|
|
||||||
+2
-2
@@ -19,7 +19,7 @@ macro_rules! rs_errln {
|
|||||||
"\x1b[0m",
|
"\x1b[0m",
|
||||||
format!($($arg)*)
|
format!($($arg)*)
|
||||||
);
|
);
|
||||||
process::exit(-1);
|
process::exit(1);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,6 +32,6 @@ macro_rules! errln {
|
|||||||
"\x1b[0m",
|
"\x1b[0m",
|
||||||
format!($($arg)*)
|
format!($($arg)*)
|
||||||
);
|
);
|
||||||
process::exit(-1);
|
std::process::exit(1);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
+3
-3
@@ -1,4 +1,4 @@
|
|||||||
use crate::rs_println;
|
use crate::{rs_println, errln};
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
@@ -25,13 +25,13 @@ pub fn start(args: String) -> PyResult<()> {
|
|||||||
let empty = CString::new("").unwrap();
|
let empty = CString::new("").unwrap();
|
||||||
|
|
||||||
let app: Py<PyAny> = PyModule::from_code(py, &app_path, &empty, &empty)?
|
let app: Py<PyAny> = PyModule::from_code(py, &app_path, &empty, &empty)?
|
||||||
.getattr("run")?
|
.getattr("main")?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
return app.call0(py);
|
return app.call0(py);
|
||||||
});
|
});
|
||||||
|
|
||||||
if from_python.is_err() { println!("py: {:?}", from_python); }
|
if from_python.is_err() { errln!("pyO3: {:?}", from_python); }
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-6
@@ -1,5 +1,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
from macros import *
|
from macros import *
|
||||||
import bot as botPy
|
import bot as botPy
|
||||||
@@ -20,11 +22,14 @@ def main():
|
|||||||
py_print("Reading data...")
|
py_print("Reading data...")
|
||||||
data.read_data(bot)
|
data.read_data(bot)
|
||||||
|
|
||||||
if True: #bot.args["py"]:
|
if not bot.args["py"]:
|
||||||
py_print("Connecting to local websocket...")
|
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!")
|
asyncio.run(py_websocket.send_message("[Connection test] Hello from Python!"))
|
||||||
|
|
||||||
|
|
||||||
main()
|
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import websockets
|
import websockets
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from macros import py_print
|
from macros import py_print
|
||||||
import bot as botPy
|
import bot as botPy
|
||||||
|
|
||||||
ws_global = None
|
ws_global = None
|
||||||
|
is_connected = False
|
||||||
|
|
||||||
async def send_message(message: str):
|
async def send_message(message: str):
|
||||||
global ws_global
|
global ws_global
|
||||||
@@ -11,11 +14,18 @@ async def send_message(message: str):
|
|||||||
|
|
||||||
|
|
||||||
async def websocket_client(bot: botPy.Bot):
|
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:
|
async with websockets.connect(f"ws://127.0.0.1:{bot.args["port"]}") as ws:
|
||||||
ws_global = ws
|
ws_global = ws
|
||||||
|
is_connected = True
|
||||||
py_print(f"Connected webSocket server on ws://127.0.0.1:{bot.args["port"]}")
|
py_print(f"Connected webSocket server on ws://127.0.0.1:{bot.args["port"]}")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
response = await ws.recv()
|
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))
|
||||||
@@ -65,7 +65,6 @@ async fn handle_message(msg: tungstenite::protocol::Message, args: Args) {
|
|||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
if !REPLY_HELLO {
|
if !REPLY_HELLO {
|
||||||
rs_println!("Replying to python...");
|
|
||||||
send_msg("[Connection test] Hello from Rust!").await;
|
send_msg("[Connection test] Hello from Rust!").await;
|
||||||
REPLY_HELLO = true;
|
REPLY_HELLO = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user