Establish a WebSocket connection by calling connect(uri, opts). The URI can be a standard WebSocket URL (ws:// or wss://) or a Unix domain socket (unix:).
URI Formats:
- Standard:
ws://host:port/path or wss://host:port/path - Unix Socket:
wss://unix:/path/to/socket:port/path
Connection Options (opts):
protocols: A string or a table of strings representing Sec-WebSocket-Protocol values.origin: The Origin header value.host: Custom Host header value.key: Custom Sec-WebSocket-Key.headers: A table of custom HTTP headers to include in the handshake.ssl_verify: SSL verification setting.server_name: SSL server_name (must be a string).client_cert & client_priv_key: Used for TLS client certificate authentication.pool, pool_size, backlog: Options for connection pooling.
Returns 1, nil, header on success (where header is the HTTP response), or nil, err on failure.
local ok, res, err = wb:connect("ws://example.com/socket", {
headers = {
["X-Custom-Header"] = "value"
},
origin = "http://example.com"
})
if not ok then
ngx.log(ngx.ERR, "connection failed: ", err)
return
end