1. 代码如下:
local server =require"resty.websocket.server"
local wb, err = server:new{
timeout =5000,
max_payload_len =65535
}
ifnot wb then
ngx.log(ngx.ERR,"failed to new websocket: ", err)
return ngx.exit(444)
end
whiletruedo
local data, typ, err = wb:recv_frame()
if wb.fatal then
ngx.log(ngx.ERR,"failed to receive frame: ", err)
return ngx.exit(444)
end
if typ =="close"thenbreak
elseif typ =="text"then
local bytes, err = wb:send_text(data)
ifnot bytes then
ngx.log(ngx.ERR,"failed to send text: ", err)
return ngx.exit(444)
end
end
end
wb:send_close()
2. 参考文档:
https://github.com/openresty/lua-resty-websocket
时间: 2024-11-03 05:21:00