### Example Usage of resty.websocket.client (Lua) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown This comprehensive example demonstrates the basic workflow for using the resty.websocket.client. It covers instantiating a client, connecting to a WebSocket server, receiving a frame, sending a text frame, and sending a close frame. It includes basic error handling for each step. ```lua 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, res = 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 ``` -------------------------------- ### Configure lua_package_path for Installation (Nginx) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown This Nginx configuration snippet shows how to set the lua_package_path directive within the http block. It adds the path to the lua-resty-websocket library's 'lib' directory to ngx_lua's search path, allowing Lua code to find and load the library. ```nginx # nginx.conf http { lua_package_path "/path/to/lua-resty-websocket/lib/?.lua;;"; ... } ``` -------------------------------- ### Importing resty.websocket.client Module (Lua) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown This snippet shows how to load the resty.websocket.client module into a Lua script, making its functions and methods available for creating and interacting with WebSocket clients. ```lua local client = require "resty.websocket.client" ``` -------------------------------- ### Loading WebSocket Protocol Module (Lua) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Loads the `resty.websocket.protocol` module, providing functions for building and receiving raw WebSocket frames. ```Lua local protocol = require "resty.websocket.protocol" ``` -------------------------------- ### Handling WebSocket Connections with resty.websocket.server in Lua Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown This snippet demonstrates how to initialize a WebSocket server instance, receive frames, handle different frame types (close, ping, pong, text, binary), send frames, and manage timeouts using the `resty.websocket.server` module. It includes error handling for initialization and frame reception/sending, showing how to respond to control frames like ping and close. ```Lua 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 ``` -------------------------------- ### Require lua-resty-websocket Library (Lua) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown This Lua code snippet demonstrates how to load the 'resty.websocket.server' module into a local variable. This is done after configuring the lua_package_path in Nginx, making the library's functions available for use in your Lua scripts. ```lua local server = require "resty.websocket.server" ``` -------------------------------- ### Connect to WebSocket with Options (wss) - lua-resty-websocket - Lua Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Connects the WebSocket client to a remote service using the `wss://` scheme for secure connections, providing an `options` table to customize connection parameters like protocols, origin, pooling, headers, SSL verification, client certificates, etc. Returns `ok` (boolean), `err` (string), and the handshake response (string) or 'connection reused'. ```Lua ok, err, res = wb:connect("wss://:/", options) ``` -------------------------------- ### Connect to WebSocket with Options (ws) - lua-resty-websocket - Lua Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Connects the WebSocket client to a remote service using the `ws://` scheme, providing an `options` table to customize connection parameters like protocols, origin, pooling, headers, etc. Returns `ok` (boolean), `err` (string), and the handshake response (string) or 'connection reused'. ```Lua ok, err, res = wb:connect("ws://:/", options) ``` -------------------------------- ### Sending Binary Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sends a binary frame over the WebSocket connection. Identical to the `send_binary` method for `resty.websocket.server` objects. Returns bytes sent or error. ```Lua bytes, err = wb:send_binary(data) ``` -------------------------------- ### Building Raw WebSocket Frame (lua-resty-websocket Protocol) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Builds a raw WebSocket frame byte string based on the final flag, opcode, payload length, payload data, and masking flag. ```Lua frame = protocol.build_frame(fin, opcode, payload_len, payload, masking) ``` -------------------------------- ### Connect to WebSocket (wss) - lua-resty-websocket - Lua Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Connects the WebSocket client to a remote service using the `wss://` scheme for secure connections. This method performs the handshake and may reuse existing connections from the pool. Returns `ok` (boolean), `err` (string), and the handshake response (string) or 'connection reused'. ```Lua ok, err, res = wb:connect("wss://:/") ``` -------------------------------- ### Connect to WebSocket (ws) - lua-resty-websocket - Lua Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Connects the WebSocket client to a remote service using the `ws://` scheme. This method performs the handshake and may reuse existing connections from the pool. Returns `ok` (boolean), `err` (string), and the handshake response (string) or 'connection reused'. ```Lua ok, err, res = wb:connect("ws://:/") ``` -------------------------------- ### Setting WebSocket Keepalive (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Puts the connection into the `ngx_lua` cosocket pool. Configures max idle timeout (ms) and pool size per worker. Returns `1` on success or `nil` and an error string on failure. Calling this makes the object enter the `closed` state. ```Lua ok, err = wb:set_keepalive(max_idle_timeout, pool_size) ``` -------------------------------- ### Setting WebSocket Timeout (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sets the timeout for the WebSocket connection, identical to the `set_timeout` method for `resty.websocket.server` objects. ```Lua wb:set_timeout(ms) ``` -------------------------------- ### Sending Pong Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sends a pong frame over the WebSocket connection, optionally with a message. Identical to the `send_pong` method for `resty.websocket.server` objects. Returns bytes sent or error. ```Lua bytes, err = wb:send_pong() ``` ```Lua bytes, err = wb:send_pong(msg) ``` -------------------------------- ### Sending Ping Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sends a ping frame over the WebSocket connection, optionally with a message. Identical to the `send_ping` method for `resty.websocket.server` objects. Returns bytes sent or error. ```Lua bytes, err = wb:send_ping() ``` ```Lua bytes, err = wb:send_ping(msg) ``` -------------------------------- ### Sending Text Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sends a text frame over the WebSocket connection. Identical to the `send_text` method for `resty.websocket.server` objects. Returns bytes sent or error. ```Lua bytes, err = wb:send_text(text) ``` -------------------------------- ### Receiving WebSocket Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Receives a WebSocket frame from the connection. Identical to the `recv_frame` method for `resty.websocket.server` objects. Returns the payload data, frame type, and error. ```Lua data, typ, err = wb:recv_frame() ``` -------------------------------- ### Receiving Raw WebSocket Frame (lua-resty-websocket Protocol) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Receives a raw WebSocket frame directly from a socket object. Allows specifying max payload length and forcing masking. Returns the payload data, frame type, and error. ```Lua data, typ, err = protocol.recv_frame(socket, max_payload_len, force_masking) ``` -------------------------------- ### Sending Raw WebSocket Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sends a raw WebSocket frame with specified final flag, opcode, and payload. Identical to the `send_frame` method for `resty.websocket.server` objects. Unmasked frames can be sent by setting `send_unmasked` in the constructor. Returns bytes sent or error. ```Lua bytes, err = wb:send_frame(fin, opcode, payload) ``` -------------------------------- ### Sending Close Frame (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Sends a close frame over the WebSocket connection, optionally with a status code and message. Identical to the `send_close` method for `resty.websocket.server` objects. Returns bytes sent or error. ```Lua bytes, err = wb:send_close() ``` ```Lua bytes, err = wb:send_close(code, msg) ``` -------------------------------- ### Closing WebSocket Connection (lua-resty-websocket Client) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown Closes the current WebSocket connection. Automatically sends a `close` frame if one hasn't been sent already. Returns `ok` (1 on success) and `err` (string on error). ```Lua ok, err = wb:close() ``` -------------------------------- ### Disable ngx_lua Socket Error Logging (Nginx) Source: https://github.com/openresty/lua-resty-websocket/blob/master/README.markdown This Nginx configuration directive disables the automatic error logging performed by ngx_lua for socket errors. It is recommended to turn this off if you are implementing custom error handling within your Lua code to avoid duplicate logging. ```nginx lua_socket_log_errors off; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.