websocket client for garry's mod servers, inspired by FredyH/GWSockets
place gmsv_tungstenite.dll (windows) or gmsv_tungstenite_linux64.dll (linux) into your garry's mod servers garrysmod/lua/bin folder
you can find examples to how interact with this library in examples/ folder
require("tungstenite")
-- connect to a websocket server
local ws = tungstenite.connect("wss://example.com/ws")
-- callback: connection established
function ws:on_connect()
print("Connected!")
ws:send("Hello, server!")
end
-- callback: received message from server
function ws:on_message(message)
print("Received:", message)
end
-- callback: error occurred
function ws:on_error(err)
print("Error:", err)
end
-- callback: disconnected
function ws:on_disconnect(reason)
print("Disconnected:", reason)
end
-- send message to server
ws:send("Hello!")
-- close connection gracefully (waits for queue)
ws:close()
-- close connection immediately
ws:close_now()
-- reopen connection (reconnect)
ws:open()connects to a websocket server and returns an instance of tungstenite, otherwise errors
sends data to server, returns nothing, errors if failed to send (can happen if socket was closed)
alias of tungstenite:send
closes connection and waits until all messages in queue are received/sent
forcefully closes connection and discards receive/send queue
re-opens (basically reconnect) websocket
called when a text message is received from the server
called when an error occurs
called when the connection is closed. whenever on_disconnect is called, socket should be considered as closed
called when the connection is successfully established