Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
api:internet:zh [2023/10/19 10:28]
hfsr [Internet API]
api:internet:zh [2024/03/27 13:10] (current)
hfsr
Line 1: Line 1:
 Internet(因特网) API Internet(因特网) API
 ========== ==========
-此库包含因特网卡的功能。另请参阅[[component:​internet|因特网组件]]以了解更多底层功能 +此库封装了因特网卡的功能。 ​  
-(例如查询HTTP可用性TCP功能)。+另请参阅[[component:​internet:zh|因特网组件]]以了解更多底层功能(例如查询HTTP可用性以及TCP功能)。
  
 - `internet.request(url:​ string[, data: string or table[, headers: table[, method: string]]]): function`  ​ - `internet.request(url:​ string[, data: string or table[, headers: table[, method: string]]]): function`  ​
-  向指定URL发送带有POST数据(如果有)的HTTP请求。如果没有POST数据,则会发送GET请求。POST数据有两种格式可选:字符串和表。如果是字符串,则按原样发送。如果是一个表,它将被转换为一个字符串,假设每个键都是一个POST变量的名称,其对应值该变量的值。方法可以显式指定为GET、POST或PUT等值。比如: +  向指定URL发送带有POST数据(如果有)的HTTP请求。如果没有POST数据,则会发送GET请求。 ​  
-  `internet.request(url,​ {some = "​variable",​ another = 1})`   +  ​POST数据有两种格式可选:字符串和表。如果是字符串,则按原样发送。如果是表,它将被转换为字符串,方式是将各个键作为POST变量的名称,其对应值该变量的值。 ​  
-  ​会发送 `some=variable&​another=1`  +`method`参数可以显式指定为GET、POST或PUT等值。 ​  
-  返回的函数是对结果块的迭代器,使用方式如下:  +  ​一些样例: ​  
-  ​`for chunk in internet.request(...) do stuff() end`   +  * `internet.request(url,​ {some = "​variable",​ another = 1})`会发送 `some=variable&​another=1`。\\ 
-  注意, **此方法也支持HTTPS**所以只需要用 +  * 指定使用PUT的样例:`internet.request("​https://​example.com",​ "put data", {}, "​PUT"​)`。\\ 
-  ​`internet.request("​https://​example.com"​)` ​通过HTTPS发送请求 +  返回的函数是对结果块的迭代器,使用方式:`for chunk in internet.request(...) do stuff() end`   
-   +  注意,**此方法也支持HTTPS**所以只需要用`internet.request("​https://​example.com"​)`可通过HTTPS发送请求。   
-  ​使用PUT: +\\
-  `internet.request("​https://​example.com",​ "put data", {}, "​PUT"​)`. +
 - `internet.socket(address:​string[,​ port:​number]):​table`  ​ - `internet.socket(address:​string[,​ port:​number]):​table`  ​
-  使用因特网组件的`connect`方法打开一个TCP套接字,并将其装为一个表,表提供了与使用 `filesystem.open` 打开的文件相同的方法: `read`、`write`与`close`(还有`seek`,但通常会执行失败)。我们更推荐使用 `internet.open` 函数,此函数会将打开的套接字[[api:​buffer:​zh|缓冲]],与 `io.open` 函数装文件的方式相同。 +  使用因特网组件的`connect`方法打开一个TCP套接字,并将其装为一个表,表提供了与使用`filesystem.open`打开的文件相同的方法:`read`、`write`与`close`(还有`seek`,但通常会执行失败)。我们更推荐使用`internet.open`函数,此函数会将打开的套接字[[api:​buffer:​zh|缓冲]],与 `io.open` 函数装文件的方式相同。 ​  
-  返回的套接字上的读取方法是*非阻塞*的。读取结果会立刻返回,但是若没有内容可读取则可能返回空字符串。写入操作*可能会*被阻塞,直到所有数据均被成功写入,但是*通常而言*会立刻返回。+  返回的套接字上的读取方法是**非阻塞**的。读取结果会立刻返回,但是若没有内容可读取则可能返回空字符串。写入操作**可能会**被阻塞,直到所有数据均被成功写入,但是**通常而言**会立刻返回。 ​  
 +\\
 - `internet.open(address:​string[,​ port:​number]):​table`  ​ - `internet.open(address:​string[,​ port:​number]):​table`  ​
-  打开一个到指定地址的带缓冲套接字流。此流可读可写,只需使用 `s:read` 和 `s:write` 方法——通常可以与 `io.open` 打开的文件类似对待。通常需要使用`s:​setTimeout(seconds)`设定缓冲区读取超时时间,以防止操作被无限期阻塞。 +  打开一个到指定地址的带缓冲套接字流。此流可读可写,分别使用`s:​read`和`s:​write`方法——通常可以与`io.open`打开的文件类似对待。通常需要使用`s:​setTimeout(seconds)`设定缓冲区读取超时时间,以防止操作被无限期阻塞。 ​  
-  返回的缓冲区上的读取操作是*阻塞式*的。读取操作会等待有可读取的数据出现,并将其返回。+  返回的缓冲区上的读取操作是**阻塞式**的。读取操作会等待有可读取的数据出现,并将其返回。 ​ 
   使用例:   使用例:
  
Line 32: Line 31:
 ``` ```
 如果你需要HTTP响应码、消息和响应头,这些数据可从内部对象中取回。该对象被存储于返回对象的元表中。 如果你需要HTTP响应码、消息和响应头,这些数据可从内部对象中取回。该对象被存储于返回对象的元表中。
 +
 ```lua ```lua
 -- https://​github.com/​kikito/​inspect.lua/​blob/​master/​inspect.lua -- https://​github.com/​kikito/​inspect.lua/​blob/​master/​inspect.lua
Line 53: Line 53:
 ``` ```
  
-下面提供了一个简单的IRC聊天机器人样例,机器人会复读你说的话。样例使用了internet(因特网) ​api提供的套接字。+下面提供了一个简单的IRC聊天机器人样例,机器人会复读你说的话。样例使用了internet(因特网) ​API提供的套接字。
 ```lua ```lua
 --这是一个简易的拆分函数,用于拆分信息 --这是一个简易的拆分函数,用于拆分信息
Line 68: Line 68:
  
 local net = require("​internet"​) local net = require("​internet"​)
-local con = net.open("​irc.esper.net",​6667) --define server / port here, this will connect to the server+local con = net.open("​irc.esper.net",​6667) --在此处设定服务器与端口,将会连接到服务器
 if(con) then if(con) then
  local line,​png,​linesplt,​msgfrom = ""​  local line,​png,​linesplt,​msgfrom = ""​
  while(true) do  while(true) do
- line = con:read() --read a line from the socket+ line = con:read() --从套接字中读取一行
  print(line)  print(line)
  linesplt = split(line,"​[^:​]+"​)  linesplt = split(line,"​[^:​]+"​)
  if #linesplt >= 2 and string.find(linesplt[2],​ "No Ident response"​) ~= nil then  if #linesplt >= 2 and string.find(linesplt[2],​ "No Ident response"​) ~= nil then
  print("​JOIN"​)  print("​JOIN"​)
- con:​write("​USER " .. nickname .. " 0 * :" .. nickname .. "​\r\n"​) --con:​write(msg) ​is used to send messages, ​con:​read() ​will read a line + con:​write("​USER " .. nickname .. " 0 * :" .. nickname .. "​\r\n"​) --con:​write(msg)用于发送信息,con:read()会读取一行 
- con:​write("​NICK " .. nickname .. "​\r\n"​) --for IRC, remember to append the \r\n on the end of all messages+ con:​write("​NICK " .. nickname .. "​\r\n"​) --IRC而言,请记住在所有信息末尾追加\r\n
  con:​write("​JOIN :" .. channel .. "​\r\n"​)  con:​write("​JOIN :" .. channel .. "​\r\n"​)
  elseif linesplt[1] == "​PING"​ or linesplt[1] == "PING " then  elseif linesplt[1] == "​PING"​ or linesplt[1] == "PING " then
  print("​PING"​)  print("​PING"​)
  png = split(line,"​[^:​]+"​)  png = split(line,"​[^:​]+"​)
- con:​write("​PONG :"​..png[#​png].."​\r\n"​) --respond to pings so we don't get disconnected+ con:​write("​PONG :"​..png[#​png].."​\r\n"​) --对ping作出响应,以保证不会断联
  elseif string.find(linesplt[1],​ "​PRIVMSG #") ~= nil then  elseif string.find(linesplt[1],​ "​PRIVMSG #") ~= nil then
  msgfrom = split(linesplt[1],"​[^ ]+")  msgfrom = split(linesplt[1],"​[^ ]+")