2021-01-19 09:20:52 +00:00
|
|
|
---
|
|
|
|
module: [kind=event] websocket_closed
|
|
|
|
---
|
|
|
|
|
2023-03-15 21:52:13 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers
|
|
|
|
|
2023-03-28 09:23:28 +00:00
|
|
|
SPDX-License-Identifier: MPL-2.0
|
2023-03-15 21:52:13 +00:00
|
|
|
-->
|
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
The [`websocket_closed`] event is fired when an open WebSocket connection is closed.
|
2021-01-19 09:20:52 +00:00
|
|
|
|
|
|
|
## Return Values
|
2023-08-24 09:48:30 +00:00
|
|
|
1. [`string`]: The event name.
|
|
|
|
2. [`string`]: The URL of the WebSocket that was closed.
|
|
|
|
3. <span class="type">[`string`]|[`nil`]</span>: The [server-provided reason][close_reason]
|
|
|
|
the websocket was closed. This will be [`nil`] if the connection was closed
|
2023-07-06 22:03:22 +00:00
|
|
|
abnormally.
|
2023-08-24 09:48:30 +00:00
|
|
|
4. <span class="type">[`number`]|[`nil`]</span>: The [connection close code][close_code],
|
|
|
|
indicating why the socket was closed. This will be [`nil`] if the connection
|
2023-07-06 22:03:22 +00:00
|
|
|
was closed abnormally.
|
|
|
|
|
|
|
|
[close_reason]: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.6 "The WebSocket Connection Close Reason, RFC 6455"
|
|
|
|
[close_code]: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5 "The WebSocket Connection Close Code, RFC 6455"
|
2021-01-19 09:20:52 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
Prints a message when a WebSocket is closed (this may take a minute):
|
|
|
|
```lua
|
2021-01-23 14:58:08 +00:00
|
|
|
local myURL = "wss://example.tweaked.cc/echo"
|
2021-01-19 09:20:52 +00:00
|
|
|
local ws = http.websocket(myURL)
|
|
|
|
local event, url
|
|
|
|
repeat
|
|
|
|
event, url = os.pullEvent("websocket_closed")
|
|
|
|
until url == myURL
|
|
|
|
print("The WebSocket at " .. url .. " was closed.")
|
|
|
|
```
|