Networking
Functions for HTTP requests, sockets, and Base64 encoding.
net.send_request
Signature: net.send_request(url, headers, post_fields)
Description: Sends an HTTPS request with optional headers and POST data. The response will be delivered asynchronously to a callback registered with engine.register_on_network_callback()
. HTTPS only.
Parameters:
url (string): The URL to send the request to.
headers (string, optional): HTTP headers to include with the request.
post_fields (string, optional): Data to send in a POST request. If not provided, a GET request will be sent.
Returns: None
Example:
-- Callback function to handle network responses
function network_callback(response_data, url)
fs.write_to_file_from_buffer("dump.txt", response_data)
engine.log("Received network response: " .. m.read_string(response_data,0), 0, 255, 0, 255)
end
-- Register the network callback
engine.register_on_network_callback(network_callback)
-- Function to send an HTTP request
function send_example_request()
local url = "https://example.com/api/data"
-- Example headers
local headers = "MyLuaClient/1.0";
-- Example POST data
local post_fields = "param1=value1¶m2=value2"
-- Send the HTTP request
net.send_request(url, headers, post_fields)
engine.log("Request sent to: " .. url, 255, 255, 255, 255)
end
-- Execute the function to send the request
send_example_request()
net.resolve
Signature: net.resolve(hostname)
Description: Looks up the first IPv4 or IPv6 address for a hostname.
Parameters:
hostname (string): The hostname to resolve (e.g., "google.com").
Returns:
string: The resolved IP address, or
nil
if resolution failed.
Example:
local ip = net.resolve("google.com")
if ip then
engine.log("Resolved google.com → " .. ip, 0, 255, 0, 255)
else
engine.log("resolve failed for google.com", 255, 0, 0, 255)
end
net.create_socket
Signature: net.create_socket(ip, port)
Description: Opens a TCP connection to the specified IP address and port, returning a socket object.
Parameters:
ip (string): The IP address to connect to.
port (number): The port number to connect to.
Returns:
socket (table): A socket object with methods for sending and receiving data, or
nil
if connection failed.error (string): An error message if the connection failed.
Example:
local ip = net.resolve("google.com")
if not ip then
engine.log("resolve failed for google.com", 255, 0, 0, 255)
return
end
local sock, err = net.create_socket(ip, 80)
if not sock then
engine.log("connect failed: " .. err, 255, 0, 0, 255)
return
end
engine.log("Connected to " .. ip .. ":80", 0, 255, 0, 255)
sock:close()
socket:send
Signature: socket:send(data)
Description: Sends data on a socket created with net.create_socket()
.
Parameters:
data (string): The raw string data to send on the socket.
Returns:
bytes_sent (number): The number of bytes sent, or
nil
if the send operation failed.error (string): An error message if the send operation failed.
Example:
local ip = net.resolve("google.com")
if not ip then return end
local sock, err = net.create_socket(ip, 80)
if not sock then return end
local req = table.concat({
"GET / HTTP/1.1",
"Host: google.com",
"Connection: close",
"",
""
}, "\r\n")
local sent, serr = sock:send(req)
if not sent then
engine.log("send failed: " .. serr, 255, 0, 0, 255)
else
engine.log("Sent " .. sent .. " bytes", 0, 255, 0, 255)
end
sock:close()
socket:receive
Signature: socket:receive(maxlen)
Description: Reads up to maxlen
bytes from a socket.
Parameters:
maxlen (number): The maximum number of bytes to read.
Returns:
data (string): The received data as a string, or
nil
if the receive operation failed.error (string): An error message if the receive operation failed.
Example:
local ip = net.resolve("google.com")
if not ip then return end
local sock, err = net.create_socket(ip, 80)
if not sock then return end
local req = table.concat({
"GET / HTTP/1.1",
"Host: google.com",
"Connection: close",
"",
""
}, "\r\n")
local sent = sock:send(req)
local chunk, rerr = sock:receive(4096)
if not chunk then
engine.log("recv failed: " .. rerr, 255, 0, 0, 255)
else
local snippet = chunk:sub(1,200)
engine.log("Response snippet:\n" .. snippet, 255, 255, 255, 255)
end
sock:close()
socket:close
Signature: socket:close()
Description: Immediately closes a socket connection.
Parameters: None
Returns: None
Example:
local ip = net.resolve("google.com")
if not ip then return end
local sock, err = net.create_socket(ip, 80)
if not sock then return end
sock:close()
engine.log("Socket closed", 255, 255, 255, 255)
net.base64_encode
Signature: net.base64_encode(lstring)
Description: Encodes a string using Base64 encoding.
Parameters:
lstring (string): The string data to encode.
Returns:
string: The Base64-encoded string.
Example:
local encoded = net.base64_encode("user123:secr3t")
engine.log("Base64 encoded: " .. encoded, 0, 255, 0, 255)
net.base64_decode
Signature: net.base64_decode(lstring)
Description: Decodes a Base64-encoded string.
Parameters:
lstring (string): The Base64-encoded string to decode.
Returns:
string: The decoded string.
Example:
local decoded = net.base64_decode("SGVsbG8sIHdvcmxkIQ==")
engine.log("Decoded: " .. decoded, 0, 255, 0, 255)
Last updated
Was this helpful?