-
Notifications
You must be signed in to change notification settings - Fork 1
AddonLibNetwork
Angelo Geels edited this page May 3, 2015
·
2 revisions
Provides access to networking functions.
-
net:openurl(url)Opens the given URL in a browser. -
net.http:get(url)Performs a GET request on the given URL, returning the result as a string. -
net.http:post(url, data, contentType)Performs a POST request on the given URL with the given data as its payload, returning the result as a string.contentTypedefaults toapplication/x-www-form-urlencoded. -
net.http:get_async(url, callback<string>)Performs a GET request on the given URL. When the request has finished, it calls the callback method with the result as its parameter. -
net.http:post_async(url, data, contentType)Performs a POST request on the given URL with the given data as its payload. When the request has finished, it calls the callback method with the result as its parameter.contentTypedefaults toapplication/x-www-form-urlencoded. -
net.http:urlencode(string)Encodes the given string for inserting into URLs. -
net.sock:socket_tcp()Creates a TCP socket and returns its handle. -
net.sock:socket_udp()Creates a UDP socket and returns its handle. -
net.sock:connect(handle, strHost, iPort)Connects the given socket with the given host and port number. -
net.sock:bind(handle, strHost, iPort)Binds the given socket to the given host and port number. -
net.sock:close(handle)Closes the given socket. -
net.sock:send(handle, strData)Sends a string of data to the given socket. -
net.sock:sendto(handle, strData, strHost, iPort)Sends a string of data to the given socket, with a destination designated by the given host and port number. -
net.sock:recv(handle, iMaxSize)Receives N bytes of data from the socket and returns it as a string. -
net.sock:recvfrom(handle, iMaxSize)Receives N bytes of data from the socket and returns it in a table containing the keysdataandendpoint, which are both a string.endpointcould be (for example)192.168.0.123:4567 -
net.sock:available(handle)Returns how many bytes are reported to be available for reading by the socket.
This library requires the NETWORK permission.
-- send some text to the API
local data = 'username=' .. net.http:urlencode(username)
data = data .. '&password=' .. net.http:urlencode(password)
data = data .. '&text=' .. net.http:urlencode(text)
net.http:post('https://example.com/send', data)
-- get some data asynchronously
net.http:get_async('https://example.com/poll', function(result)
print('We got some data: "' .. result .. '"')
end)-- just as an example, perform a simple http request
local socket = net.sock:socket_tcp()
net.sock:connect(socket, 'example.com', 80)
net.sock:send(socket,
"GET / HTTP/1.1\n" ..
"Host: example.com\n" ..
"Connection: close\n" ..
"User-Agent: ExampleCode / JustUseNetHttpForHttpRequestsPlease\n\n"
)
print(net.sock:recv(socket, 50))
net.sock:close()