mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 04:50:36 +00:00
Use standard method calling conventions for websockets
This commit is contained in:
parent
309c461e11
commit
8abfc21181
3 changed files with 4 additions and 46 deletions
|
@ -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>(
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue