Skip to main content

core.websocket

Coroutine-based WebSocket client for Pragtical Code Editor. Supports ws/wss connections, subprotocol negotiation, fragmented messages, automatic pong replies, optional keepalive pings, reconnect backoff, session restore hooks, and async callbacks.

local websocket = require "core.websocket"

client​

(field) client: websocket.client

websocket.client​

__index​

(field) __index: websocket.client

_close_received​

(field) _close_received: boolean

_close_request_code​

(field) _close_request_code: unknown

_close_request_reason​

(field) _close_request_reason: unknown

_close_requested​

(field) _close_requested: table

_close_sent​

(field) _close_sent: boolean

_conn​

(field) _conn: net.tcp

A TCP network connection.


_did_open​

(field) _did_open: boolean

_last_ping_time​

(field) _last_ping_time: number|unknown

_manual_close​

(field) _manual_close: table

_message_opcode​

(field) _message_opcode: unknown

_message_parts​

(field) _message_parts: table

_recv_buffer​

(field) _recv_buffer: string?

_remote_close_code​

(field) _remote_close_code: integer?

_remote_close_reason​

(field) _remote_close_reason: string?

_send_queue​

(field) _send_queue: table

error​

(field) error: string?

protocol​

(field) protocol: string?

reconnect_attempt​

(field) reconnect_attempt: integer

response_info​

(field) response_info: (websocket.response_info)?

status​

(field) status: "closed"|"closing"|"connecting"|"open"|"reconnecting"

url​

(field) url: string

new​

function websocket.client.new(url: any, options: websocket.connect.options)
-> websocket.client

@param options: websocket.connect.options

@return: websocket.client


_build_session_info​

(method) websocket.client:_build_session_info(attempt: integer, previous_outcome?: websocket.disconnect_info)
-> websocket.session_info

@param previous_outcome?: websocket.disconnect_info

@return: websocket.session_info


_drop_stale_control_frames​

(method) websocket.client:_drop_stale_control_frames()

_flush_send_queue​

(method) websocket.client:_flush_send_queue(touch: fun(), should_abort: fun():string?)
-> boolean?
2. string?

_get_reconnect_delay​

(method) websocket.client:_get_reconnect_delay(attempt: integer)
-> number

_handle_frame​

(method) websocket.client:_handle_frame(frame: table)
-> did_work: boolean
2. should_stop: boolean
3. err: string?

@return did_work: boolean

@return should_stop: boolean

@return err: string?


_pump_incoming​

(method) websocket.client:_pump_incoming(touch: fun())
-> did_work: boolean
2. should_stop: boolean
3. err: string?

@return did_work: boolean

@return should_stop: boolean

@return err: string?


_reset_session_state​

(method) websocket.client:_reset_session_state()

_run​

(method) websocket.client:_run()

_run_session​

(method) websocket.client:_run_session(parsed: table, session_info: websocket.session_info)
-> websocket.disconnect_info

@param session_info: websocket.session_info

@return: websocket.disconnect_info


_should_reconnect​

(method) websocket.client:_should_reconnect(outcome: websocket.disconnect_info, attempt: integer)
-> boolean

@param outcome: websocket.disconnect_info


_wait_reconnect​

(method) websocket.client:_wait_reconnect(delay: number)
-> string?

close​

(method) websocket.client:close(code?: integer, reason?: string)
-> boolean?
2. string?

Close the connection.


get_status​

(method) websocket.client:get_status()
-> status: "closed"|"closing"|"connecting"|"open"|"reconnecting"
2. errmsg: string?

Get the current connection status.

status:
| "connecting"
| "reconnecting"
| "open"
| "closing"
| "closed"

@return status: "closed"|"closing"|"connecting"|"open"|"reconnecting"

@return errmsg: string?


ping​

(method) websocket.client:ping(payload?: string)
-> boolean?
2. string?

Queue a ping frame for sending.


send​

(method) websocket.client:send(data: string)
-> boolean?
2. string?

Queue a text message for sending.


send_binary​

(method) websocket.client:send_binary(data: string)
-> boolean?
2. string?

Queue a binary message for sending.


send_text​

(method) websocket.client:send_text(data: string)
-> boolean?
2. string?

Queue a text message for sending.


websocket.connect.options​

headers​

(field) headers: table<string, string>?

is_cancelled​

(field) is_cancelled: (fun():boolean)?

max_reconnect_attempts​

(field) max_reconnect_attempts: integer?

Maximum reconnect attempts. Unlimited when nil.


on_close​

(field) on_close: fun(client: websocket.client, code?: integer, reason?: string, was_clean: boolean)?

on_connect​

(field) on_connect: fun(client: websocket.client, info: websocket.response_info, session: websocket.session_info)?

on_error​

(field) on_error: fun(client: websocket.client, err: string)?

on_message​

(field) on_message: fun(client: websocket.client, message: string, is_binary: boolean)?

on_ping​

(field) on_ping: fun(client: websocket.client, payload: string)?

on_pong​

(field) on_pong: fun(client: websocket.client, payload: string)?

on_reconnect​

(field) on_reconnect: fun(client: websocket.client, info: websocket.reconnect_info)?

ping_interval​

(field) ping_interval: number?

Seconds between automatic ping frames.


ping_payload​

(field) ping_payload: string?

Payload used for automatic ping frames.


protocols​

(field) protocols: (string|string[])?

reconnect​

(field) reconnect: boolean?

Retry automatically after disconnections.


reconnect_backoff​

(field) reconnect_backoff: number?

Backoff multiplier. Default: 2


reconnect_delay​

(field) reconnect_delay: number?

Initial reconnect delay in seconds. Default: 1


reconnect_delay_max​

(field) reconnect_delay_max: number?

Maximum reconnect delay in seconds. Default: 30


reconnect_jitter​

(field) reconnect_jitter: number?

Random delay jitter in seconds. Default: 0


restore_session​

(field) restore_session: fun(client: websocket.client, info: websocket.session_info, response: websocket.response_info)?

should_reconnect​

(field) should_reconnect: fun(client: websocket.client, info: websocket.disconnect_info):boolean??

timeout​

(field) timeout: number?

Inactivity timeout in seconds.


websocket.disconnect_info​

attempt​

(field) attempt: integer

close_code​

(field) close_code: integer?

close_reason​

(field) close_reason: string

error​

(field) error: string?

opened​

(field) opened: boolean

was_clean​

(field) was_clean: boolean

websocket.header_value​

websocket.on_close​

websocket.on_connect​

websocket.on_error​

websocket.on_message​

websocket.on_ping​

websocket.on_pong​

websocket.on_reconnect​

websocket.reconnect_info​

attempt​

(field) attempt: integer

close_code​

(field) close_code: integer?

close_reason​

(field) close_reason: string

delay​

(field) delay: number

error​

(field) error: string?

is_reconnect​

(field) is_reconnect: boolean

opened​

(field) opened: boolean

previous_close_code​

(field) previous_close_code: integer?

previous_close_reason​

(field) previous_close_reason: string?

previous_error​

(field) previous_error: string?

previous_was_clean​

(field) previous_was_clean: boolean?

was_clean​

(field) was_clean: boolean

websocket.response_info​

headers​

(field) headers: table<string, string|string[]>

protocol​

(field) protocol: string?

status​

(field) status: integer

url​

(field) url: string

websocket.restore_session​

websocket.session_info​

attempt​

(field) attempt: integer

is_reconnect​

(field) is_reconnect: boolean

previous_close_code​

(field) previous_close_code: integer?

previous_close_reason​

(field) previous_close_reason: string?

previous_error​

(field) previous_error: string?

previous_was_clean​

(field) previous_was_clean: boolean?

websocket.should_reconnect​

websocket.status​

websocket.status:
| "connecting"
| "reconnecting"
| "open"
| "closing"
| "closed"

connect​

function core.websocket.connect(url: string, options?: websocket.connect.options)
-> client: websocket.client

Open a WebSocket connection asynchronously.

@param options?: websocket.connect.options

@return client: websocket.client