mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 04:50:36 +00:00
Update websocket tests and types to use new calling convention
This commit is contained in:
parent
8abfc21181
commit
138221b93e
5 changed files with 19 additions and 19 deletions
|
@ -24,10 +24,10 @@ local handle = net.serve(PORT, {
|
||||||
return "unreachable"
|
return "unreachable"
|
||||||
end,
|
end,
|
||||||
handleWebSocket = function(socket)
|
handleWebSocket = function(socket)
|
||||||
local socketMessage = socket.next()
|
local socketMessage = socket:next()
|
||||||
assert(socketMessage == REQUEST, "Invalid web socket request from client")
|
assert(socketMessage == REQUEST, "Invalid web socket request from client")
|
||||||
socket.send(RESPONSE)
|
socket:send(RESPONSE)
|
||||||
socket.close()
|
socket:close()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -43,19 +43,19 @@ end)
|
||||||
|
|
||||||
local socket = net.socket(WS_URL)
|
local socket = net.socket(WS_URL)
|
||||||
|
|
||||||
socket.send(REQUEST)
|
socket:send(REQUEST)
|
||||||
|
|
||||||
local socketMessage = socket.next()
|
local socketMessage = socket:next()
|
||||||
assert(socketMessage ~= nil, "Got no web socket response from server")
|
assert(socketMessage ~= nil, "Got no web socket response from server")
|
||||||
assert(socketMessage == RESPONSE, "Invalid web socket response from server")
|
assert(socketMessage == RESPONSE, "Invalid web socket response from server")
|
||||||
|
|
||||||
socket.close()
|
socket:close()
|
||||||
|
|
||||||
task.cancel(thread2)
|
task.cancel(thread2)
|
||||||
|
|
||||||
-- Wait for the socket to close and make sure we can't send messages afterwards
|
-- Wait for the socket to close and make sure we can't send messages afterwards
|
||||||
task.wait()
|
task.wait()
|
||||||
local success3, err2 = (pcall :: any)(socket.send, "")
|
local success3, err2 = (pcall :: any)(socket.send, socket, "")
|
||||||
assert(not success3, "Sending messages after the socket has been closed should error")
|
assert(not success3, "Sending messages after the socket has been closed should error")
|
||||||
local message2 = tostring(err2)
|
local message2 = tostring(err2)
|
||||||
assert(
|
assert(
|
||||||
|
|
|
@ -8,17 +8,17 @@ assert(type(socket.send) == "function", "send must be a function")
|
||||||
assert(type(socket.close) == "function", "close must be a function")
|
assert(type(socket.close) == "function", "close must be a function")
|
||||||
|
|
||||||
-- Request to close the socket
|
-- Request to close the socket
|
||||||
socket.close()
|
socket:close()
|
||||||
|
|
||||||
-- Drain remaining messages, until we got our close message
|
-- Drain remaining messages, until we got our close message
|
||||||
while socket.next() do
|
while socket:next() do
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(type(socket.closeCode) == "number", "closeCode should exist after closing")
|
assert(type(socket.closeCode) == "number", "closeCode should exist after closing")
|
||||||
assert(socket.closeCode == 1000, "closeCode should be 1000 after closing")
|
assert(socket.closeCode == 1000, "closeCode should be 1000 after closing")
|
||||||
|
|
||||||
local success, message = pcall(function()
|
local success, message = pcall(function()
|
||||||
socket.send("Hello, world!")
|
socket:send("Hello, world!")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(not success, "send should fail after closing")
|
assert(not success, "send should fail after closing")
|
||||||
|
|
|
@ -8,7 +8,7 @@ local task = require("@lune/task")
|
||||||
local socket = net.socket("wss://gateway.discord.gg/?v=10&encoding=json")
|
local socket = net.socket("wss://gateway.discord.gg/?v=10&encoding=json")
|
||||||
|
|
||||||
while not socket.closeCode do
|
while not socket.closeCode do
|
||||||
local response = socket.next()
|
local response = socket:next()
|
||||||
|
|
||||||
if response then
|
if response then
|
||||||
local decodeSuccess, decodeMessage = pcall(serde.decode, "json" :: "json", response)
|
local decodeSuccess, decodeMessage = pcall(serde.decode, "json" :: "json", response)
|
||||||
|
@ -23,6 +23,6 @@ while not socket.closeCode do
|
||||||
|
|
||||||
-- Close the connection after a second with the success close code
|
-- Close the connection after a second with the success close code
|
||||||
task.wait(1)
|
task.wait(1)
|
||||||
socket.close(1000)
|
socket:close(1000)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@ local socket = net.socket("wss://gateway.discord.gg/?v=10&encoding=json")
|
||||||
|
|
||||||
local spawnedThread = task.spawn(function()
|
local spawnedThread = task.spawn(function()
|
||||||
while not socket.closeCode do
|
while not socket.closeCode do
|
||||||
socket.next()
|
socket:next()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -23,9 +23,9 @@ end)
|
||||||
task.wait(1)
|
task.wait(1)
|
||||||
|
|
||||||
local payload = '{"op":1,"d":null}'
|
local payload = '{"op":1,"d":null}'
|
||||||
socket.send(payload)
|
socket:send(payload)
|
||||||
socket.send(buffer.fromstring(payload))
|
socket:send(buffer.fromstring(payload))
|
||||||
socket.close(1000)
|
socket:close(1000)
|
||||||
|
|
||||||
task.cancel(delayedThread)
|
task.cancel(delayedThread)
|
||||||
task.cancel(spawnedThread)
|
task.cancel(spawnedThread)
|
||||||
|
|
|
@ -173,9 +173,9 @@ export type ServeHandle = {
|
||||||
]=]
|
]=]
|
||||||
export type WebSocket = {
|
export type WebSocket = {
|
||||||
closeCode: number?,
|
closeCode: number?,
|
||||||
close: (code: number?) -> (),
|
close: (self: WebSocket, code: number?) -> (),
|
||||||
send: (message: (string | buffer)?, asBinaryMessage: boolean?) -> (),
|
send: (self: WebSocket, message: (string | buffer)?, asBinaryMessage: boolean?) -> (),
|
||||||
next: () -> string?,
|
next: (self: WebSocket) -> string?,
|
||||||
}
|
}
|
||||||
|
|
||||||
--[=[
|
--[=[
|
||||||
|
|
Loading…
Reference in a new issue