diff --git a/API-Internet.md b/API-Internet.md index 556f4f6..8db091c 100644 --- a/API-Internet.md +++ b/API-Internet.md @@ -1,7 +1,5 @@ -This library wraps functionality of Internet cards. +This library wraps functionality of Internet cards. Also see the [[Internet Component|Component/Internet]] for more low level functionality (such as querying availability of HTTP and TCP functionality). -- `internet.isHttpEnabled():boolean` - Returns whether HTTP requests are allowed on the server (config setting). - `internet.request(url: string[, data: string or table]): function` Sends an HTTP request to the specified URL, with the specified POST data, if any. If no data is specified, a GET request will be made. The POST data can be in one of two formats: if it's a string, it will be sent as-is. If it's a table, it will be converted to a string by assuming that each key is the name of a POST variable, and it's associated value is the value for that variable. So, for example: `internet.request(url, {some = "variable", another = 1})` @@ -9,21 +7,14 @@ This library wraps functionality of Internet cards. The returned function is an iterator over the lines of the result, use it like so: `for line in internet.request(...) do stuff() end` **Important**: you should not call `os.sleep`, `event.pull` or any other functions that directly or indirectly consume signals while iterating the result lines, since the lines of the response are one signal each (to avoid running out of memory for large results). -- `internet.isTcpEnabled():boolean` - Returns whether TCP sockets are allowed on the server (config setting). -- `internet.connect(address:string[, port:number]):number` - Opens a new TCP connection. Returns the handle of the connection. The returned handle can be used to interact with the opened socket using the other callbacks. This can error if TCP sockets are not enabled, there are too many open connections or some other I/O error occurs. -- `internet.read(handle:number,n:number):string` - Tries to read data from the socket stream. Returns the read byte array. Takes the handle returned from internet.connect -- `internet.write(handle:number, data:string): number` - Tries to write data to the socket stream. Returns the number of bytes written. Takes the handle returned by internet.connect -- `internet.close(handle:number)` - Closes the socket with the specified handle. - -Note for internet.open, you may also do this: +- `internet.socket(address:string[, port:number]):table` + Opens a TCP socket using an internet component's `connect` method and wraps it in a table that provides the same methods as a file opened using `filesystem.open`: `read`, `write` and `close` (and `seek`, which will always fail). It is recommended to use `internet.open` instead, which will wrap the opened socket in a [[buffer|API/buffer]], the same way `io.open` wraps files. +- `internet.open(address:string[, port:number]):table` + Opens a buffered socket stream to the specified address. The stream can be read from and written from, using `s:read` and `s:write` - in general it can be treated much like files opened using `io.open`. It may often be desirable to set the buffer's read timeout using `s:setTimeout(seconds)`, to avoid it blocking indefinitely. Example usage: ```lua -local handle = internet.connect(...) -data = handle:read(10) +local internet = require("internet") +local handle = internet.open("example.com", 1337) +local data = handle:read(10) handle:write("1234") handle:close() ```