websocket
local websocket = require 'beast'.websocket
local tls = require 'tls'
local host = 'example.com'
local ws = websocket.new(tls.dial(host .. ':https'))
ws:handshake(host, '/')
local text = nil
local buf = byte_span.new(ws:read_size_hint())
repeat
print('reading...')
local nread = ws:read_some(buf)
text = byte_span.append(text, buf:slice(1, nread))
until ws.is_message_done
print(text)
Functions
new(socket: ip.tcp.socket|tls.socket) → websocket
Constructor.
Previous socket
object is moved-from and becomes an invalid object.
get_option(self, opt: string) → value
Get an option from the socket.
Currently available options are:
"timeout: { handshake_timeout: number, idle_timeout: number, keep_alive_pings: boolean }"
-
Stream option to control the behavior of websocket timeouts.
"permessage_deflate: { client_enable: boolean, client_max_window_bits: integer, client_no_context_takeover: boolean, compLevel: integer, memLevel: integer, msg_size_threshold: integer, server_enable: boolean, server_max_window_bits: integer, server_no_context_takeover: boolean }"
-
permessage-deflate extension options.
set_option(self, opt: string, val)
Set an option on the socket.
Currently available options are:
"timeout: "client"|"server"|{ handshake_timeout: "none"|number|nil, idle_timeout: "none"|number|nil, keep_alive_pings: boolean|nil }"
-
Stream option to control the behavior of websocket timeouts.
"permessage_deflate: { client_enable: boolean|nil, client_max_window_bits: integer|nil, client_no_context_takeover: boolean|nil, compLevel: integer|nil, memLevel: integer|nil, msg_size_threshold: integer|nil, server_enable: boolean|nil, server_max_window_bits: integer|nil, server_no_context_takeover: boolean|nil }"
-
permessage-deflate extension options.
close(self)
function close(self) (1)
function close(self, reason_code: integer) (2)
function close(self, reason_string: string) (3)
function close(self, reason_code: integer, reason_string: string) (4)
Send a websocket close control frame.
handshake(self, host: string, target: string[, extra_headers: { [string]: string }])
Perform the WebSocket handshake in the client role.
You can send extra headers in the opening handshake through the extra_headers
parameter. Optional headers such as "origin"
aren’t added by default. A list
of common headers that might be of interest follows:
"origin"
-
Compare the
"origin"
to the"host"
to block malicious scripts coming from web browsers. "cookie"
-
Authentication token.
"sec-websocket-protocol"
-
If present, this value indicates one or more comma-separated subprotocols the client wishes to speak, ordered by preference.