网站维护经费,怎么能够找到免费的网址,重庆哪里有做淘宝网站推广的,wordpress中文主题框架想用lua实现与http服务器的通信#xff0c;请求一些数据会回来#xff0c;默认lua.socket.http是同步的#xff0c;所以想弄一个异步的方式
测试环境
lua 5.1
同步
以下是同步的代码#xff0c;其中http.request会被阻塞住的
local function send_request()local res,…想用lua实现与http服务器的通信请求一些数据会回来默认lua.socket.http是同步的所以想弄一个异步的方式
测试环境
lua 5.1
同步
以下是同步的代码其中http.request会被阻塞住的
local function send_request()local res, code, response_headers http.request(http://www.lua.org/,POST,nameLuaage100,{[Content-Type] application/x-www-form-urlencoded})print(code , code)
end
send_request();输出结果:
F:\study\lua\fragmentary lua f:\study\lua\fragmentary\socket\client.lua
code 200如果每次执行一次请求就卡住我们逻辑的Tick那整个客户端就卡在那里了。所以我需要非阻塞的用法。也就是执行请求之后不用等http服务器返回结果而是继续向下执行。我主动监听有返回了再回调我的逻辑。
异步
参考[2]给出了一定的思路但是我是看到GPT给的结果代码都详细列出来了直呼牛皮啊 local host www.baidu.comlocal path ;local data [[nameLuaage100]]local port 80;local con assert(socket.connect(host, port))self.Conn:settimeout(0)local request string.format(POST /%s HTTP/1.0\r\nHost: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s,path, host, #data, data)con:send(request)local response while true dolocal chunk, status, partial con:receive(1024)response response .. (chunk or partial)print(status , status);if status closed thenbreak;endend输出
status nil
status nil
status closed小结
1.主要是用tcp连接用非阻塞的方式去实现2.自己通过receive接口获取数据
参考
[1]socket.http
[2]Non-Preemptive Multithreading