Update websocket tests and types to use new calling convention

This commit is contained in:
Filip Tibell 2024-10-16 22:00:33 +02:00
parent 8abfc21181
commit 138221b93e
No known key found for this signature in database
5 changed files with 19 additions and 19 deletions

View file

@ -24,10 +24,10 @@ local handle = net.serve(PORT, {
return "unreachable"
end,
handleWebSocket = function(socket)
local socketMessage = socket.next()
local socketMessage = socket:next()
assert(socketMessage == REQUEST, "Invalid web socket request from client")
socket.send(RESPONSE)
socket.close()
socket:send(RESPONSE)
socket:close()
end,
})
@ -43,19 +43,19 @@ end)
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 == RESPONSE, "Invalid web socket response from server")
socket.close()
socket:close()
task.cancel(thread2)
-- Wait for the socket to close and make sure we can't send messages afterwards
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")
local message2 = tostring(err2)
assert(

View file

@ -8,17 +8,17 @@ assert(type(socket.send) == "function", "send must be a function")
assert(type(socket.close) == "function", "close must be a function")
-- Request to close the socket
socket.close()
socket:close()
-- Drain remaining messages, until we got our close message
while socket.next() do
while socket:next() do
end
assert(type(socket.closeCode) == "number", "closeCode should exist after closing")
assert(socket.closeCode == 1000, "closeCode should be 1000 after closing")
local success, message = pcall(function()
socket.send("Hello, world!")
socket:send("Hello, world!")
end)
assert(not success, "send should fail after closing")

View file

@ -8,7 +8,7 @@ local task = require("@lune/task")
local socket = net.socket("wss://gateway.discord.gg/?v=10&encoding=json")
while not socket.closeCode do
local response = socket.next()
local response = socket:next()
if response then
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
task.wait(1)
socket.close(1000)
socket:close(1000)
end
end

View file

@ -10,7 +10,7 @@ local socket = net.socket("wss://gateway.discord.gg/?v=10&encoding=json")
local spawnedThread = task.spawn(function()
while not socket.closeCode do
socket.next()
socket:next()
end
end)
@ -23,9 +23,9 @@ end)
task.wait(1)
local payload = '{"op":1,"d":null}'
socket.send(payload)
socket.send(buffer.fromstring(payload))
socket.close(1000)
socket:send(payload)
socket:send(buffer.fromstring(payload))
socket:close(1000)
task.cancel(delayedThread)
task.cancel(spawnedThread)

View file

@ -173,9 +173,9 @@ export type ServeHandle = {
]=]
export type WebSocket = {
closeCode: number?,
close: (code: number?) -> (),
send: (message: (string | buffer)?, asBinaryMessage: boolean?) -> (),
next: () -> string?,
close: (self: WebSocket, code: number?) -> (),
send: (self: WebSocket, message: (string | buffer)?, asBinaryMessage: boolean?) -> (),
next: (self: WebSocket) -> string?,
}
--[=[