Skip to content

Commit

Permalink
Use standard method calling conventions for websockets
Browse files Browse the repository at this point in the history
filiptibell committed Oct 16, 2024
1 parent 309c461 commit 8abfc21
Showing 3 changed files with 4 additions and 46 deletions.
4 changes: 2 additions & 2 deletions crates/lune-std-net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -65,9 +65,9 @@ async fn net_request(lua: &Lua, config: RequestConfig) -> LuaResult<LuaTable> {
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()?;
NetWebSocket::new(ws).into_lua_table(lua)
NetWebSocket::new(ws).into_lua(lua)
}

async fn net_serve<'lua>(
4 changes: 2 additions & 2 deletions crates/lune-std-net/src/server/service.rs
Original file line number Diff line number Diff line change
@@ -40,13 +40,13 @@ impl Service<Request<Incoming>> for Svc {
lua.spawn_local(async move {
let sock = sock.await.unwrap();
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 =
keys.websocket_handler(&lua_inner).unwrap().unwrap();

lua_inner
.push_thread_back(handler_websocket, lua_tab)
.push_thread_back(handler_websocket, lua_val)
.unwrap();
});

42 changes: 0 additions & 42 deletions crates/lune-std-net/src/websocket.rs
Original file line number Diff line number Diff line change
@@ -23,29 +23,6 @@ use hyper_tungstenite::{
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)]
pub struct NetWebSocket<T> {
close_code_exists: Arc<AtomicBool>,
@@ -125,25 +102,6 @@ where
let mut ws = self.write_stream.lock().await;
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>

0 comments on commit 8abfc21

Please sign in to comment.