Use standard method calling conventions for websockets

This commit is contained in:
Filip Tibell 2024-10-16 21:55:53 +02:00
parent 309c461e11
commit 8abfc21181
No known key found for this signature in database
3 changed files with 4 additions and 46 deletions

View file

@ -65,9 +65,9 @@ async fn net_request(lua: &Lua, config: RequestConfig) -> LuaResult<LuaTable> {
res.await?.into_lua_table(lua) res.await?.into_lua_table(lua)
} }
async fn net_socket(lua: &Lua, url: String) -> LuaResult<LuaTable> { async fn net_socket(lua: &Lua, url: String) -> LuaResult<LuaValue> {
let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?; let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?;
NetWebSocket::new(ws).into_lua_table(lua) NetWebSocket::new(ws).into_lua(lua)
} }
async fn net_serve<'lua>( async fn net_serve<'lua>(

View file

@ -40,13 +40,13 @@ impl Service<Request<Incoming>> for Svc {
lua.spawn_local(async move { lua.spawn_local(async move {
let sock = sock.await.unwrap(); let sock = sock.await.unwrap();
let lua_sock = NetWebSocket::new(sock); let lua_sock = NetWebSocket::new(sock);
let lua_tab = lua_sock.into_lua_table(&lua_inner).unwrap(); let lua_val = lua_sock.into_lua(&lua_inner).unwrap();
let handler_websocket: LuaFunction = let handler_websocket: LuaFunction =
keys.websocket_handler(&lua_inner).unwrap().unwrap(); keys.websocket_handler(&lua_inner).unwrap().unwrap();
lua_inner lua_inner
.push_thread_back(handler_websocket, lua_tab) .push_thread_back(handler_websocket, lua_val)
.unwrap(); .unwrap();
}); });

View file

@ -23,29 +23,6 @@ use hyper_tungstenite::{
WebSocketStream, WebSocketStream,
}; };
use lune_utils::TableBuilder;
// Wrapper implementation for compatibility and changing colon syntax to dot syntax
const WEB_SOCKET_IMPL_LUA: &str = r#"
return freeze(setmetatable({
close = function(...)
return websocket:close(...)
end,
send = function(...)
return websocket:send(...)
end,
next = function(...)
return websocket:next(...)
end,
}, {
__index = function(self, key)
if key == "closeCode" then
return websocket.closeCode
end
end,
}))
"#;
#[derive(Debug)] #[derive(Debug)]
pub struct NetWebSocket<T> { pub struct NetWebSocket<T> {
close_code_exists: Arc<AtomicBool>, close_code_exists: Arc<AtomicBool>,
@ -125,25 +102,6 @@ where
let mut ws = self.write_stream.lock().await; let mut ws = self.write_stream.lock().await;
ws.close().await.into_lua_err() ws.close().await.into_lua_err()
} }
pub fn into_lua_table(self, lua: &Lua) -> LuaResult<LuaTable> {
let setmetatable = lua.globals().get::<_, LuaFunction>("setmetatable")?;
let table_freeze = lua
.globals()
.get::<_, LuaTable>("table")?
.get::<_, LuaFunction>("freeze")?;
let env = TableBuilder::new(lua)?
.with_value("websocket", self.clone())?
.with_value("setmetatable", setmetatable)?
.with_value("freeze", table_freeze)?
.build_readonly()?;
lua.load(WEB_SOCKET_IMPL_LUA)
.set_name("websocket")
.set_environment(env)
.eval()
}
} }
impl<T> LuaUserData for NetWebSocket<T> impl<T> LuaUserData for NetWebSocket<T>