在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):openresty/lua-resty-websocket开源软件地址(OpenSource Url):https://github.com/openresty/lua-resty-websocket开源编程语言(OpenSource Language):Lua 98.1%开源软件介绍(OpenSource Introduction):Namelua-resty-websocket - Lua WebSocket implementation for the ngx_lua module Table of Contents
StatusThis library is considered production ready. DescriptionThis Lua library implements a WebSocket server and client libraries based on the ngx_lua module. This Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior. Note that only RFC 6455 is supported. Earlier protocol revisions like "hybi-10", "hybi-07", and "hybi-00" are not and will not be considered. Synopsis local server = require "resty.websocket.server"
local wb, err = server:new{
timeout = 5000, -- in milliseconds
max_payload_len = 65535,
}
if not wb then
ngx.log(ngx.ERR, "failed to new websocket: ", err)
return ngx.exit(444)
end
local data, typ, err = wb:recv_frame()
if not data then
if not string.find(err, "timeout", 1, true) then
ngx.log(ngx.ERR, "failed to receive a frame: ", err)
return ngx.exit(444)
end
end
if typ == "close" then
-- for typ "close", err contains the status code
local code = err
-- send a close frame back:
local bytes, err = wb:send_close(1000, "enough, enough!")
if not bytes then
ngx.log(ngx.ERR, "failed to send the close frame: ", err)
return
end
ngx.log(ngx.INFO, "closing with status code ", code, " and message ", data)
return
end
if typ == "ping" then
-- send a pong frame back:
local bytes, err = wb:send_pong(data)
if not bytes then
ngx.log(ngx.ERR, "failed to send frame: ", err)
return
end
elseif typ == "pong" then
-- just discard the incoming pong frame
else
ngx.log(ngx.INFO, "received a frame of type ", typ, " and payload ", data)
end
wb:set_timeout(1000) -- change the network timeout to 1 second
bytes, err = wb:send_text("Hello world")
if not bytes then
ngx.log(ngx.ERR, "failed to send a text frame: ", err)
return ngx.exit(444)
end
bytes, err = wb:send_binary("blah blah blah...")
if not bytes then
ngx.log(ngx.ERR, "failed to send a binary frame: ", err)
return ngx.exit(444)
end
local bytes, err = wb:send_close(1000, "enough, enough!")
if not bytes then
ngx.log(ngx.ERR, "failed to send the close frame: ", err)
return
end Modulesresty.websocket.serverTo load this module, just do this local server = require "resty.websocket.server" Methodsnew
Performs the websocket handshake process on the server side and returns a WebSocket server object. In case of error, it returns An optional options table can be specified. The following options are as follows:
set_timeout
Sets the timeout delay (in milliseconds) for the network-related operations. send_text
Sends the In case of errors, returns send_binary
Sends the In case of errors, returns send_ping
Sends out a In case of errors, returns Note that this method does not wait for a pong frame from the remote end. send_pong
Sends out a In case of errors, returns send_close
Sends out a In case of errors, returns For a list of valid status code, see the following document: http://tools.ietf.org/html/rfc6455#section-7.4.1 Note that this method does not wait for a send_frame
Sends out a raw websocket frame by specifying the For a list of valid opcode, see http://tools.ietf.org/html/rfc6455#section-5.2 In case of errors, returns To control the maximal payload length allowed, you can pass the To control whether to send masked frames, you can pass recv_frame
Receives a WebSocket frame from the wire. In case of an error, returns two The second return value is always the frame type, which could be one of For http://tools.ietf.org/html/rfc6455#section-7.4.1 For other types of frames, just returns the payload and the type. For fragmented frames, the resty.websocket.clientTo load this module, just do this local client = require "resty.websocket.client" A simple example to demonstrate the usage: local client = require "resty.websocket.client"
local wb, err = client:new()
local uri = "ws://127.0.0.1:" .. ngx.var.server_port .. "/s"
local ok, err = wb:connect(uri)
if not ok then
ngx.say("failed to connect: " .. err)
return
end
local data, typ, err = wb:recv_frame()
if not data then
ngx.say("failed to receive the frame: ", err)
return
end
ngx.say("received: ", data, " (", typ, "): ", err)
local bytes, err = wb:send_text("copy: " .. data)
if not bytes then
ngx.say("failed to send frame: ", err)
return
end
local bytes, err = wb:send_close()
if not bytes then
ngx.say("failed to send frame: ", err)
return
end Methodsclient:new
Instantiates a WebSocket client object. In case of error, it returns An optional options table can be specified. The following options are as follows:
client:connect
Connects to the remote WebSocket service port and performs the websocket handshake process on the client side. Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method. An optional Lua table can be specified as the last argument to this method to specify various connect options:
The SSL connection mode ( client:close
Closes the current WebSocket connection. If no client:set_keepalive
Puts the current WebSocket connection immediately into the You can specify the max idle timeout (in ms) when the connection is in the pool and the maximal size of the pool every nginx worker process. In case of success, returns Only call this method in the place you would have called the client:set_timeout
Identical to the client:send_text
Identical to the send_text method of the client:send_binary
Identical to the send_binary method of the client:send_ping
Identical to the send_ping method of the client:send_pong
Identical to the send_pong method of the client:send_close
Identical to the send_close method of the client:send_frame
Identical to the send_frame method of the To control whether to send unmasked frames, you can pass client:recv_frame
Identical to the recv_frame method of the resty.websocket.protocolTo load this module, just do this local protocol = require "resty.websocket.protocol" Methodsprotocol.recv_frame
Receives a WebSocket frame from the wire. protocol.build_frame
Builds a raw WebSocket frame. protocol.send_frame
Sends a raw WebSocket frame. Automatic Error LoggingBy default the underlying ngx_lua module does error logging when socket errors happen. If you are already doing proper error handling in your own Lua code, then you are recommended to disable this automatic error logging by turning off ngx_lua's lua_socket_log_errors directive, that is, lua_socket_log_errors off; Limitations
InstallationIt is recommended to use the latest OpenResty bundle directly where this library
is bundled and enabled by default. At least OpenResty 1.4.2.9 is required. And you need to enable LuaJIT when building your OpenResty
bundle by passing the If you want to use this library with your own Nginx build (with ngx_lua), then you need to ensure you are using at least ngx_lua 0.9.0 (and lua-bitop library if you are not using LuaJIT). Also, You need to configure the lua_package_path directive to add the path of your lua-resty-websocket source tree to ngx_lua's Lua module search path, as in # nginx.conf
http {
lua_package_path "/path/to/lua-resty-websocket/lib/?.lua;;";
...
} and then load the library in Lua: local server = require "resty.websocket.server" TODOCommunityEnglish Mailing ListThe openresty-en mailing list is for English speakers. |