This library provides a simple interface for working with WebSockets in Lua. It allows you to create WebSocket connections, send and receive messages, and handle various events.
- Easy-to-use WebSocket connection management
- Event-based handling for open, close, error, and message events
- Support for optional headers and cookies
- Asynchronous operation with a tick-based update system
To use this WebSocket library, follow these steps:
-
Download the
.dll
files from this repository. -
Place the
.dll
files in the appropriate directory for your game. For example:- For Scrap Mechanic:
C:\Program Files (x86)\Steam\steamapps\common\Scrap Mechanic\Release
- For other games, consult the game's modding documentation to find the correct directory
- For Scrap Mechanic:
-
Once the DLL is in place, you can import the WebSocket library in your Lua script using:
Here's a basic example of how to use the websocket library:
local ws = require("websocket")
-- Optional headers and cookies
local optional_headers = {A = "Hello", B = "World"}
local optional_cookies = {Cookie_1 = "Value_1"}
-- Create a connection
local connection = ws:connect("wss://127.0.0.1:8080", optional_headers, optional_cookies)
-- Event handlers
connection:on("open", function()
print("Opened connection")
connection:send("Waddup bro")
end)
connection:on("close", function(code, reason)
print("Closed connection", code, reason)
end)
connection:on("error", function(code, reason)
print("Connection errored", code, reason)
end)
connection:on("message", function(data)
print("message: ", data)
connection:send(data .. tostring(math.random(9)))
end)
-- In your main loop or update function, call:
ws:tick()
-- To close the connection:
connection:close()
Establishes a WebSocket connection.
url
: The WebSocket server URL (e.g., "wss://127.0.0.1:8080")headers
: (optional) A table of additional headerscookies
: (optional) A table of cookies
Returns a connection object.
Registers an event handler.
event
: String, one of "open", "close", "error", or "message"callback
: Function to be called when the event occurs
Sends data through the WebSocket connection.
data
: The data to send
Closes the WebSocket connection.
Updates the WebSocket system. Should be called regularly (e.g., in a game loop or update function).