mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
31 lines
779 B
Lua
31 lines
779 B
Lua
--> A basic web socket server that echoes given messages
|
|
|
|
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
|
|
|
|
-- Run the server on port 8080
|
|
|
|
local handle = net.serve(PORT, {
|
|
handleWebSocket = function(socket)
|
|
print("Got new web socket connection!")
|
|
repeat
|
|
local message = socket.next()
|
|
if message ~= nil then
|
|
socket.send("Echo\n" .. message)
|
|
end
|
|
until message == nil
|
|
print("Web socket disconnected.")
|
|
end,
|
|
})
|
|
|
|
print(`Listening on port {PORT} 🚀`)
|
|
|
|
-- Exit our example after a small delay, if you copy this
|
|
-- example just remove this part to keep the server running
|
|
|
|
task.delay(2, function()
|
|
print("Shutting down...")
|
|
task.wait(1)
|
|
handle.stop()
|
|
end)
|