#
hs.socket
Talk to custom protocols using asynchronous TCP sockets.
For UDP sockets see hs.socket.udp
.
hs.socket
is implemented with CocoaAsyncSocket. CocoaAsyncSocket's tagging features provide a handy way to implement custom protocols.
For example, you can easily implement a basic HTTP client as follows (though using hs.http
is recommended for the real world):
local TAG_HTTP_HEADER, TAG_HTTP_CONTENT = 1, 2
local body = ""
local function httpCallback(data, tag)
if tag == TAG_HTTP_HEADER then
print(tag, "TAG_HTTP_HEADER"); print(data)
local contentLength = data:match("\r\nContent%-Length: (%d+)\r\n")
client:read(tonumber(contentLength), TAG_HTTP_CONTENT)
elseif tag == TAG_HTTP_CONTENT then
print(tag, "TAG_HTTP_CONTENT"); print(data)
body = data
end
end
client = hs.socket.new(httpCallback):connect("google.com", 80)
client:write("GET /index.html HTTP/1.0\r\nHost: google.com\r\n\r\n")
client:read("\r\n\r\n", TAG_HTTP_HEADER)
Resulting in the following console output (adjust log verbosity with hs.socket.setLogLevel()
) :
LuaSkin: (secondary thread): TCP socket connected
LuaSkin: (secondary thread): Data written to TCP socket
LuaSkin: (secondary thread): Data read from TCP socket
1 TAG_HTTP_HEADER
HTTP/1.0 301 Moved Permanently
Location: http://www.google.com/index.html
Content-Type: text/html; charset=UTF-8
Date: Thu, 03 Mar 2016 08:38:02 GMT
Expires: Sat, 02 Apr 2016 08:38:02 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 229
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
LuaSkin: (secondary thread): Data read from TCP socket
2 TAG_HTTP_CONTENT
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/index.html">here</A>.
</BODY></HTML>
LuaSkin: (secondary thread): TCP socket disconnected Socket closed by remote peer
#
Submodules
#
API Overview
Variables - Configurable values
timeout
Functions - API calls offered directly by the extension
parseAddress
Constructors - API calls which return an object, typically one that offers API methods
new server
Methods - API calls which can only be made on an object returned by a constructor
connect connected connections disconnect info listen read receive send setCallback setTimeout startTLS write
#
API Documentation
#
Variables
#
timeout
#
Functions
#
parseAddress
#
Constructors
#
new
#
server
#
Methods
#
connect
#
connected
#
connections
#
disconnect
#
info
#
listen
#
read
#
receive
#
send
#
setCallback
| | |
| --------------------------------------------|-------------------------------------------------------------------------------------|
| Signature | hs.socket:setCallback([fn]) -> self
|
| Type | Method |
| Description | Sets the read callback for the socket. |
| Parameters |
fn
- An optional callback function to process data read from the socket.nil
or no argument clears the callback. The callback receives 2 parameters:data
- The data read from the socket as a string.tag
- The integer tag associated with the read call, which defaults to-1
.
- The
object.hs.socket
- A callback must be set in order to read data from the socket.