Differences

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

Link to this comparison view

Next revision
Previous revision
api:internet:zh [2022/08/11 10:29]
fight_xing created
api:internet:zh [2024/03/27 13:10] (current)
hfsr
Line 1: Line 1:
-Internet API+Internet(因特网) ​API
 ========== ==========
- +此库封装了因特网卡功能。   
-**本篇文章部分/​全部内容还没有进行翻译** +另请参阅[[component:​internet:zh|因特网组件]]以了解更多底层功能(例如查询HTTP可用性以及TCP功能)。
- +
-This library wraps functionality of Internet cards. Also see the [[component:​internet|Internet Component]] for more low level functionality (such as querying availability of HTTP and TCP functionality).+
  
 - `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`  ​
-  ​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. **method** can be explicitly specified to values such as GETPOST, or PUT. Some examples: +  ​向指定URL发送带有POST数据(如果有)的HTTP请求。如果没有POST数据,则会发送GET请求。 ​  
-  `internet.request(url,​ {some = "​variable",​ another = 1})`   +  ​POST数据有两种格式可选:字符串和表。如果是字符串,则按原样发送。如果是表,它将被转换为字符串,方式是将各个键作为POST变量的名称,其对应值为该变量的值。 ​  
-  Will send `some=variable&​another=1`.  ​ +`method`参数可以显式指定为GETPOSTPUT等值。  ​ 
-  ​The returned function is an iterator over chunks of the resultuse it like so:  ​ +  ​一些样例: ​  
-  `for chunk in internet.request(...) do stuff() end`   +  * `internet.request(url,​ {some = "​variable",​ another = 1})`会发送 ​`some=variable&​another=1`。\\ 
-  ​Note that **this method ALSO support ​HTTPS**. So simply use  +  ​* 指定使用PUT的样例:`internet.request("​https://​example.com"​"put data", {}, "​PUT"​)`。\\ 
-  ​`internet.request("​https://​example.com"​)` ​to send a request through ​HTTPS+  ​返回的函数是对结果块的迭代器,使用方式:`for chunk in internet.request(...) do stuff() end`   
-   +  ​注意,**此方法也支持HTTPS**。所以只需要用`internet.request("​https://​example.com"​)`即可通过HTTPS发送请求。 ​  
-  ​Example specifying PUT: +\\
-  `internet.request("​https://​example.com",​ "put data", {}, "​PUT"​)`. +
 - `internet.socket(address:​string[,​ port:​number]):​table`  ​ - `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 [[api:​buffer|buffer]], the same way `io.open` ​wraps files. ​  +  ​使用因特网组件的`connect`方法打开一个TCP套接字,并将其封装为一个表,此表提供了与使用`filesystem.open`打开的文件相同的方法:`read``write``close`(还有`seek`,但通常会执行失败)。我们更推荐使用`internet.open`函数,此函数会将打开的套接字封装进[[api:​buffer:zh|缓冲]],与 ​`io.open` ​函数封装文件的方式相同。 ​  
-  ​The read method on the returned socket is *non-blocking*. Read will instantly return, but may return an empty string if there is nothing to read. Write *mayblock until all data has been successfully written. It'​ll ​*usuallyreturn immediately,​ though.+  ​返回的套接字上的读取方法是**非阻塞**的。读取结果会立刻返回,但是若没有内容可读取则可能返回空字符串。写入操作**可能会**被阻塞,直到所有数据均被成功写入,但是**通常而言**会立刻返回。 ​  
 +\\
 - `internet.open(address:​string[,​ port:​number]):​table`  ​ - `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. ​ +  ​打开一个到指定地址的带缓冲套接字流。此流可读可写,分别使用`s:read``s:write`方法——通常可以与`io.open`打开的文件类似对待。通常需要使用`s:​setTimeout(seconds)`设定缓冲区读取超时时间,以防止操作被无限期阻塞。  ​ 
-  ​The read method on the returned buffer is *blocking*. Read will wait until some data is available to be read and return that.   +  ​返回的缓冲区上的读取操作是**阻塞式**的。读取操作会等待有可读取的数据出现,并将其返回。 ​  
-  ​Example usage:+  ​使用例:
  
 ```lua ```lua
Line 33: Line 30:
 handle:​close()  ​ handle:​close()  ​
 ``` ```
-If you need the HTTP response code, message, and headers, they are retrieved from the internal object, which is stored in the metatable of the returned object.+如果你需要HTTP响应码、消息和响应头,这些数据可从内部对象中取回。该对象被存储于返回对象的元表中。 
 ```lua ```lua
 -- https://​github.com/​kikito/​inspect.lua/​blob/​master/​inspect.lua -- https://​github.com/​kikito/​inspect.lua/​blob/​master/​inspect.lua
Line 42: Line 40:
 local result = ""​ local result = ""​
 for chunk in handle do result = result..chunk end for chunk in handle do result = result..chunk end
--- Print the body of the HTTP response+-- 输出HTTP响应的body
 -- print(result) -- print(result)
    
--- Grab the metatable for the handle. This contains the +-- 从句柄处取得元表,其中包含内部的HTTPRequest对象
--- internal ​HTTPRequest ​object.+
 local mt = getmetatable(handle) local mt = getmetatable(handle)
    
--- The response ​method grabs the information for +-- response方法取得了HTTP响应码、响应信息和响应头的信息
--- the HTTP response code, the response message, and the +
--- response headers.+
 local code, message, headers = mt.__index.response() local code, message, headers = mt.__index.response()
 print("​code = "​..tostring(code)) print("​code = "​..tostring(code))
Line 58: Line 53:
 ``` ```
  
-This is an example of a basic IRC bot that echos back what you say to it, using the sockets in the internet ​api.+下面提供了一个简单的IRC聊天机器人样例,机器人会复读你说的话。样例使用了internet(因特网) API提供的套接字。
 ```lua ```lua
---this is just a basic split function we'll use to split the messages+--这是一个简易的拆分函数,用于拆分信息
 function split(data, pat) function split(data, pat)
  local ret = {}  local ret = {}
Line 68: Line 63:
  return ret  return ret
 end end
---config+--配置
 local nickname = "​myircbot"​ local nickname = "​myircbot"​
 local channel = "#​mybotchannel"​ local channel = "#​mybotchannel"​
  
 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],"​[^ ]+")
Line 100: Line 95:
 ``` ```
  
-For a more advanced example, check out the IRC Client program available in the latest release of OpenComputers[irc.lua](https://​github.com/​MightyPirates/​OpenComputers/​blob/​master-MC1.7.10/​src/​main/​resources/​assets/​opencomputers/​loot/​irc/​usr/​bin/​irc.lua)+若要获取更高级的使用例,请查看最新版本的OpenComputers提供的IRC客户端程序:[irc.lua](https://​github.com/​MightyPirates/​OpenComputers/​blob/​master-MC1.7.10/​src/​main/​resources/​assets/​opencomputers/​loot/​irc/​usr/​bin/​irc.lua)
  
 目录 目录