From 138221b93e0d1cb1bce2d06d190d30b0e25f5a24 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 16 Oct 2024 22:00:33 +0200 Subject: [PATCH] Update websocket tests and types to use new calling convention --- tests/net/serve/websockets.luau | 14 +++++++------- tests/net/socket/basic.luau | 6 +++--- tests/net/socket/wss.luau | 4 ++-- tests/net/socket/wss_rw.luau | 8 ++++---- types/net.luau | 6 +++--- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/net/serve/websockets.luau b/tests/net/serve/websockets.luau index ad53a38..51aea82 100644 --- a/tests/net/serve/websockets.luau +++ b/tests/net/serve/websockets.luau @@ -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( diff --git a/tests/net/socket/basic.luau b/tests/net/socket/basic.luau index 2eb4a3b..aad61c5 100644 --- a/tests/net/socket/basic.luau +++ b/tests/net/socket/basic.luau @@ -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") diff --git a/tests/net/socket/wss.luau b/tests/net/socket/wss.luau index 6594d08..74afba3 100644 --- a/tests/net/socket/wss.luau +++ b/tests/net/socket/wss.luau @@ -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 diff --git a/tests/net/socket/wss_rw.luau b/tests/net/socket/wss_rw.luau index fefb244..2f9d96c 100644 --- a/tests/net/socket/wss_rw.luau +++ b/tests/net/socket/wss_rw.luau @@ -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) diff --git a/types/net.luau b/types/net.luau index e9b793e..7a7204b 100644 --- a/types/net.luau +++ b/types/net.luau @@ -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?, } --[=[