mirror of
https://github.com/lune-org/lune.git
synced 2024-12-13 21:40:40 +00:00
Improve websocket examples
This commit is contained in:
parent
620361348f
commit
5157e60379
5 changed files with 20 additions and 25 deletions
|
@ -1,6 +1,10 @@
|
||||||
--> A basic web socket client communicates with an echo server
|
--> A basic web socket client that communicates with an echo server
|
||||||
|
|
||||||
local URL = "wss://demo.piesocket.com/v3/"
|
local PORT = if process.env.PORT ~= nil and #process.env.PORT > 0
|
||||||
|
then assert(tonumber(process.env.PORT), "Failed to parse port from env")
|
||||||
|
else 8080
|
||||||
|
|
||||||
|
local URL = `ws://127.0.0.1:{PORT}`
|
||||||
|
|
||||||
-- Connect to our web socket server
|
-- Connect to our web socket server
|
||||||
|
|
||||||
|
@ -11,7 +15,7 @@ print("Sending a message every second for 5 seconds...")
|
||||||
|
|
||||||
-- Force exit after 10 seconds in case the server is not responding well
|
-- Force exit after 10 seconds in case the server is not responding well
|
||||||
|
|
||||||
task.delay(10, function()
|
local forceExit = task.delay(10, function()
|
||||||
warn("Example did not complete in time, exiting...")
|
warn("Example did not complete in time, exiting...")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
end)
|
end)
|
||||||
|
@ -32,4 +36,5 @@ end
|
||||||
print("Closing web socket...")
|
print("Closing web socket...")
|
||||||
socket.close()
|
socket.close()
|
||||||
|
|
||||||
|
task.cancel(forceExit)
|
||||||
print("Done! 🌙")
|
print("Done! 🌙")
|
||||||
|
|
|
@ -13,7 +13,7 @@ local handle = net.serve(PORT, {
|
||||||
repeat
|
repeat
|
||||||
local message = socket.next()
|
local message = socket.next()
|
||||||
if message ~= nil then
|
if message ~= nil then
|
||||||
socket.send("Echo\n" .. message)
|
socket.send("Echo - " .. message)
|
||||||
end
|
end
|
||||||
until message == nil
|
until message == nil
|
||||||
print("Web socket disconnected.")
|
print("Web socket disconnected.")
|
||||||
|
@ -25,8 +25,9 @@ print(`Listening on port {PORT} 🚀`)
|
||||||
-- Exit our example after a small delay, if you copy this
|
-- Exit our example after a small delay, if you copy this
|
||||||
-- example just remove this part to keep the server running
|
-- example just remove this part to keep the server running
|
||||||
|
|
||||||
task.delay(2, function()
|
task.delay(10, function()
|
||||||
print("Shutting down...")
|
print("Shutting down...")
|
||||||
task.wait(1)
|
task.wait(1)
|
||||||
handle.stop()
|
handle.stop()
|
||||||
|
task.wait(1)
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -169,6 +169,7 @@ declare class NetWebSocket
|
||||||
close: () -> ()
|
close: () -> ()
|
||||||
send: (message: string) -> ()
|
send: (message: string) -> ()
|
||||||
next: () -> string?
|
next: () -> string?
|
||||||
|
iter: () -> () -> string
|
||||||
function __iter(self): () -> string
|
function __iter(self): () -> string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -39,15 +39,9 @@ impl NetWebSocketClient {
|
||||||
local proxy = newproxy(true)
|
local proxy = newproxy(true)
|
||||||
local meta = getmetatable(proxy)
|
local meta = getmetatable(proxy)
|
||||||
meta.__index = {
|
meta.__index = {
|
||||||
close = function()
|
close = function() return ws:close() end,
|
||||||
return ws:close()
|
send = function(...) return ws:send(...) end,
|
||||||
end,
|
next = function() return ws:next() end,
|
||||||
send = function(...)
|
|
||||||
return ws:send(...)
|
|
||||||
end,
|
|
||||||
next = function()
|
|
||||||
return ws:next()
|
|
||||||
end,
|
|
||||||
}
|
}
|
||||||
meta.__iter = function()
|
meta.__iter = function()
|
||||||
return function()
|
return function()
|
||||||
|
@ -75,7 +69,7 @@ impl LuaUserData for NetWebSocketClient {
|
||||||
}),
|
}),
|
||||||
None => Ok(LuaValue::Nil),
|
None => Ok(LuaValue::Nil),
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,15 +39,9 @@ impl NetWebSocketServer {
|
||||||
local proxy = newproxy(true)
|
local proxy = newproxy(true)
|
||||||
local meta = getmetatable(proxy)
|
local meta = getmetatable(proxy)
|
||||||
meta.__index = {
|
meta.__index = {
|
||||||
close = function()
|
close = function() return ws:close() end,
|
||||||
return ws:close()
|
send = function(...) return ws:send(...) end,
|
||||||
end,
|
next = function() return ws:next() end,
|
||||||
send = function(...)
|
|
||||||
return ws:send(...)
|
|
||||||
end,
|
|
||||||
next = function()
|
|
||||||
return ws:next()
|
|
||||||
end,
|
|
||||||
}
|
}
|
||||||
meta.__iter = function()
|
meta.__iter = function()
|
||||||
return function()
|
return function()
|
||||||
|
@ -75,7 +69,7 @@ impl LuaUserData for NetWebSocketServer {
|
||||||
}),
|
}),
|
||||||
None => Ok(LuaValue::Nil),
|
None => Ok(LuaValue::Nil),
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue