Skip to content

Commit

Permalink
Update websocket tests and types to use new calling convention
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Oct 16, 2024
1 parent 8abfc21 commit 138221b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions tests/net/serve/websockets.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/net/socket/basic.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/net/socket/wss.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
8 changes: 4 additions & 4 deletions tests/net/socket/wss_rw.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
6 changes: 3 additions & 3 deletions types/net.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
}

--[=[
Expand Down

0 comments on commit 138221b

Please sign in to comment.