oc-ws a WebSocket library for OpenComputers.
I haven't done a lot of testing, so let me know if you get this to work or not. This is only supports the basic websocket protocol with no extensions. Here are some things that are not supported
- Masked messages from server
- Binary websocket messages
To use oc-ws in your OpenComputers project, follow these steps:
- Run the following command:
wget https://raw.githubusercontent.com/bluescorpian/oc-ws/main/src/ws.lua /usr/lib/ws.lua
- In your OpenComputers program, import the oc-ws library using the
require
function:local WebSocket = require("ws")
Here's a basic example of how to create a WebSocket client and send a message using oc-ws:
-- Import the oc-ws library
local WebSocket = require("ws")
-- Create a new WebSocket instance
local ws = WebSocket.new({
address = "ws://example.com",
port = 80,
path = "/websocket",
})
-- Connect to the WebSocket server
while true do
local connected, err = socket:finishConnect()
if connected then break end
if err then return print('Failed to connect: ' .. err) end
if event.pull(1) == 'interrupted' then return end
end
print("Connected to WebSocket server!")
-- Send a message
ws:send("Hello, WebSocket!")
-- Read incoming messages
while true do
local messageType, message, err = ws:readMessage()
if err then return print('Websocket Error: ' .. err) end
if messageType == WebSocket.MESSAGE_TYPES.TEXT then
print('Message Received: ' .. message)
elseif messageType == WebSocket.MESSAGE_TYPES.PING then
print('Ping')
ws:pong(message)
elseif messageType == WebSocket.MESSAGE_TYPES.PONG then
print('Pong')
end
if event.pull(5) == 'interrupted' then return end
end
-- Close the WebSocket connection when done
ws:close()
-
READY_STATES
: An enum representing the WebSocket connection states.CONNECTING (0)
: The WebSocket is in the process of connecting.OPEN (1)
: The WebSocket connection is open and ready to send/receive messages.CLOSING (2)
: The WebSocket connection is in the process of closing.CLOSED (3)
: The WebSocket connection is closed.
-
MESSAGE_TYPES
: An enum representing WebSocket message types.TEXT (0)
: Text message.PING (1)
: Ping message.PONG (2)
: Pong message.CLOSE (3)
: Close message.
Creates a new WebSocket instance with the specified options.
socketOptions
(table):address
(string): The WebSocket server address (required).port
(number): The WebSocket server port (default: 80).path
(string): The path for the WebSocket connection (default: '/').internet
(object): The internet component (default: primary internet component).
Returns a WebSocket instance.
Completes the WebSocket connection process, call as often as possible.
Returns a boolean indicating whether the connection was successful and an optional error message if the connection failed.
Sends a text message over the WebSocket connection.
message
(string): The message to send.
Sends a ping message over the WebSocket connection.
data
(string): Optional data to include in the ping message.
Sends a pong message over the WebSocket connection.
data
(string): Optional data to include in the pong message.
Reads a WebSocket message from the connection.
Returns the message type (TEXT, PING, PONG, CLOSE), the message content (string), and an optional error message.
Closes the WebSocket connection.
Checks if the WebSocket connection is open.
Returns true if the connection is open; otherwise, returns false.
oc-ws is licensed under the GNU Affero General Public License (AGPL).
I used feldim2425 project to help with my implementation.