此库包含因特网卡的功能。另请参阅因特网组件以了解更多底层功能 (例如查询HTTP可用性与TCP功能)。
internet.request(url: string[, data: string or table[, headers: table[, method: string]]]): function
internet.request(url, {some = "variable", another = 1})
some=variable&another=1
.for chunk in internet.request(...) do stuff() end
internet.request("https://example.com")
就可以通过HTTPS发送请求
internet.request("https://example.com", "put data", {}, "PUT")
.
internet.socket(address:string[, port:number]):table
connect
方法打开一个TCP套接字,并将其包装为一个表,表中提供了与使用 filesystem.open
打开的文件相同的方法: read
、write
与close
(还有seek
,但通常会执行失败)。我们更推荐使用 internet.open
函数,此函数会将打开的套接字包装上缓冲,与 io.open
函数包装文件的方式相同。
返回的套接字上的读取方法是非阻塞的。读取结果会立刻返回,但是若没有内容可读取则可能返回空字符串。写入操作可能会被阻塞,直到所有数据均被成功写入,但是通常而言会立刻返回。internet.open(address:string[, port:number]):table
s:read
和 s:write
方法——通常可以与 io.open
打开的文件类似对待。通常需要使用s:setTimeout(seconds)
设定缓冲区读取超时时间,以防止操作被无限期阻塞。
返回的缓冲区上的读取操作是阻塞式的。读取操作会等待有可读取的数据出现,并将其返回。
使用例:local internet = require("internet") local handle = internet.open("example.com", 1337) local data = handle:read(10) handle:write("1234") handle:close()
如果你需要HTTP响应码、消息和响应头,这些数据可从内部对象中取回。该对象被存储于返回对象的元表中。
-- https://github.com/kikito/inspect.lua/blob/master/inspect.lua local inspect = require("inspect") local internet = require("internet") local handle = internet.request("https://www.google.com") local result = "" for chunk in handle do result = result..chunk end -- 输出HTTP响应的body -- print(result) -- 从句柄处取得元表,其中包含内部的HTTPRequest对象 local mt = getmetatable(handle) -- response方法取得了HTTP响应码、响应信息和响应头的信息 local code, message, headers = mt.__index.response() print("code = "..tostring(code)) print("message = "..tostring(message)) print(inspect(headers))
下面提供了一个简单的IRC聊天机器人样例,机器人会复读你说的话。样例使用了internet(因特网) api提供的套接字。
--这是一个简易的拆分函数,用于拆分信息 function split(data, pat) local ret = {} for i in string.gmatch(data,pat) do table.insert(ret,i) end return ret end --配置 local nickname = "myircbot" local channel = "#mybotchannel" local net = require("internet") local con = net.open("irc.esper.net",6667) --define server / port here, this will connect to the server if(con) then local line,png,linesplt,msgfrom = "" while(true) do line = con:read() --read a line from the socket print(line) linesplt = split(line,"[^:]+") if #linesplt >= 2 and string.find(linesplt[2], "No Ident response") ~= nil then 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("NICK " .. nickname .. "\r\n") --for IRC, remember to append the \r\n on the end of all messages con:write("JOIN :" .. channel .. "\r\n") elseif linesplt[1] == "PING" or linesplt[1] == "PING " then print("PING") png = split(line,"[^:]+") con:write("PONG :"..png[#png].."\r\n") --respond to pings so we don't get disconnected elseif string.find(linesplt[1], "PRIVMSG #") ~= nil then msgfrom = split(linesplt[1],"[^ ]+") msgfrom = msgfrom[3] con:write("PRIVMSG "..msgfrom.." :"..linesplt[2].."\r\n") end end else print("Connection failed.") end
若要获取更高级的使用例,请查看最新版本的OpenComputers提供的IRC客户端程序:irc.lua