1. 程式人生 > 實用技巧 >在OpenWrt中利用nixio完成socket通訊

在OpenWrt中利用nixio完成socket通訊

在OpenWrt中使用socket通訊主要就是呼叫nixio.socket來完成。我們可以寫一個模組,然後在需要使用的時候使用require來引入。

說明:lua是指令碼語言,可在htm檔案內直接使用<%%>進行引用。
閱讀程式碼,其中luci.http是在/usr/lib/lua/luci目錄下安裝自帶的http.lua檔案;luci.resttemplate是我自定義的根據nixio編寫的socket通訊簡單工具類,能訪問遠端的服務,呼叫遠端的介面!
同理,在/usr/lib/lua/luci目錄下建立resttemplate.lua檔案,內容如下:

--resttemplate.lua 
core
={} local h=require "luci.http" local n=require "nixio" core.ip="192.168.80.192" core.port=8080 core.uri="/users" core.method="GET" core.charset="UTF-8" core.rcvTimeout=20 function core.get(name) end function core.httpData(data) -- core.uri=string.upper(core.uri) core.uri="/users" return core.method.." "..core.uri.." HTTP/1.1\r\n"..
"Host: "..core.ip..":"..core.port.."\r\n".. "Content-type: text/html;charset="..core.charset.."\r\n".. "Accept-Language: zh-cn\r\n".. "User-Agent: Mozilla/4.0(Compatible win32; MSIE)\r\n".. "Content-Length: "..string.len(data).."\r\n".. "Connection: close\r\n\r\n".. data end
function core.restData(method,uri,data,ip,port) data=data or "" method=method or "GET" uri=uri or "/" return method.." "..uri.." HTTP/1.1\r\n".. "Accept: */*\r\n".. "Accept-Encoding: gzip, deflate, br\r\n".. "Accept-Language: zh-CN,zh;q=0.9\r\n".. "Connection: keep-alive\r\n".. "Host: "..ip..":"..port.."\r\n".. "Content-Length: "..string.len(data).."\r\n".. data end function core.testget(data) socket:setopt("socket","rcvtimeo",timeout) socket:send(core.httpData(data)) local tt={} repeat local tmp=socket:recv(100) if tmp==false then socket:close() return false,"response timeout" end tmp=tostring(tmp) local i= #tt tt[i+1]=tmp until #tmp < 1 socket:close() return true,"success 200" end function core.send(data) local position local t={} local http local socket=nixio.socket("inet","stream") local tmp if not socket then return false, "建立socket失敗" end if not socket:connect(core.ip,core.port) then socket:close() return false, "服務無響應,連線伺服器"..core.ip..":"..core.port.."失敗" end socket:setopt("socket","rcvtimeo",core.rcvTimeout) socket:send(core.httpData(data)) repeat tmp=socket:recv(100) if tmp==false then socket:close() return false,"響應超時" end tmp=tostring(tmp) t[#t + 1] = tmp until #tmp < 1 socket:close() local result=table.concat(t) position=string.find(result,"\r\n\r\n") if position==nil then return false,"返回的資料格式不合法。資料:"..result end result=string.sub(result,string.find(result,"\r\n\r\n")+4) return result end --create a socket with ip,port,timeout function core.socket(ip,port,timeout) local socket=nixio.socket("inet","stream") if not socket then return false,"create socket('inet','stream') failed!" end if not socket:connect(ip,port) then socket:close() return false,"connect "..ip..":"..port.." failed!" end socket:setopt("socket","rcvtimeo",timeout) return socket end function core.post() end return core

使用示例:

    local h=require "luci.http"
    local c=require "luci.resttemplate"
    

--    h.write("Hello World</br>")
--    h.write("測試socket通訊失敗!<Br>")
--    h.write(c.send("luci web's page Client!"))

    h.write(c.send(""))

https://blog.csdn.net/qq_41953807/article/details/106229876