Beast
local beast = require 'beast'
This module exposes bindings for the class websocket::stream
found in the
Boost.Beast
library. You should see Boost.Beast’s documentation for an overview on how to
use these bindings.
The concept of a dynamic buffer found in Boost.Beast is ignored here. Dynamic
buffers as exposed in Boost.Beast (and Boost.Asio) are more prone to error than
usual when exposed to programmers too used to GC interfaces. Here we only use
Emilua’s byte_span
type (inspired by Golang’s slices).
beast::websocket::stream::read()
is also ignored here as it only works over
dynamic buffers. However it’s so easy to reimplement it in pure Lua that nothing
is really lost.
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)