XU XUv0.13.1
中文

#net/websocket

import: #net/websocket

counts: 5 functions, 6 methods, 2 types, 10 constants, 0 method sets

WebSocket protocol implementation (RFC 6455). Supports frame encoding/decoding, masking/unmasking, handshake key negotiation, and high-level message read/write connections for both clients and servers.

const OP_CONTINUATION

XuAPI
pub const OP_CONTINUATION = 0

Purpose

Constant OP_CONTINUATION, defining protocol or algorithm parameters.

const OP_TEXT

XuAPI
pub const OP_TEXT = 1

Purpose

Constant OP_TEXT, defining protocol or algorithm parameters.

const OP_BINARY

XuAPI
pub const OP_BINARY = 2

Purpose

Constant OP_BINARY, defining protocol or algorithm parameters.

const OP_CLOSE

XuAPI
pub const OP_CLOSE = 8

Purpose

Constant OP_CLOSE, defining protocol or algorithm parameters.

const OP_PING

XuAPI
pub const OP_PING = 9

Purpose

Constant OP_PING, defining protocol or algorithm parameters.

const OP_PONG

XuAPI
pub const OP_PONG = 10

Purpose

Constant OP_PONG, defining protocol or algorithm parameters.

const STATUS_NORMAL

XuAPI
pub const STATUS_NORMAL = 1000

Purpose

Constant STATUS_NORMAL, defining protocol or algorithm parameters.

const STATUS_GOING_AWAY

XuAPI
pub const STATUS_GOING_AWAY = 1001

Purpose

Constant STATUS_GOING_AWAY, defining protocol or algorithm parameters.

const STATUS_PROTOCOL_ERROR

XuAPI
pub const STATUS_PROTOCOL_ERROR = 1002

Purpose

Constant STATUS_PROTOCOL_ERROR, defining protocol or algorithm parameters.

const STATUS_UNSUPPORTED

XuAPI
pub const STATUS_UNSUPPORTED = 1003

Purpose

Constant STATUS_UNSUPPORTED, defining protocol or algorithm parameters.

compute_accept_key

XuAPI
pub fn compute_accept_key(challenge_key string) string

Purpose

compute_accept_key computes the Sec-WebSocket-Accept response header value from the client's Sec-WebSocket-Key challenge according to RFC 6455 §4.2.2.

Parameters

challenge_key string
challenge_key (string)

Returns

string — result string

struct Frame

XuAPI
pub struct Frame {
    pub fin bool
    pub opcode int
    pub masked bool
    pub payload string
}

Purpose

Websocket Protocol data frame encapsulation.

Public fields

fin bool
field (bool)
opcode int
field (int)
masked bool
field (bool)
payload string
field (string)

encode_frame

XuAPI
pub fn encode_frame(fin bool, opcode int, payload &string, mask bool) string

Purpose

encode_frame serializes a WebSocket data frame into wire bytes.

Parameters

fin bool
fin (bool)
opcode int
opcode (int)
payload &string
payload (read-only borrow; caller keeps the original; type &string)
mask bool
mask (bool)

Returns

string — result string

decode_frame

XuAPI
pub fn decode_frame(raw &string) Frame

Purpose

decode_frame parses a single WebSocket frame from a byte buffer. Throws on failure; handle the error or propagate it to the caller.

Parameters

raw &string
raw (read-only borrow; caller keeps the original; type &string)

Returns

Frame — a value of this type

struct Conn

XuAPI
pub struct Conn {
    pub fd int
    pub is_client bool
    pub closed bool
}

Purpose

Conn represents an active WebSocket connection over an underlying socket.

Public fields

fd int
file descriptor
is_client bool
field (bool)
closed bool
field (bool)

new_conn

XuAPI
pub fn new_conn(fd int, is_client bool) Conn

Purpose

Construct conn.

Parameters

fd int
file descriptor (int)
is_client bool
is_client (bool)

Returns

Conn — a value of this type

($&Conn) write_message

XuAPI
pub fn (c $&Conn) write_message(opcode int, payload string) int

Purpose

Operation on $&Conn: write message. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Conn
The Conn instance used by this call (exclusive borrow).
opcode int
opcode (int)
payload string
payload (string)

Returns

int — a value of this type

($&Conn) write_text

XuAPI
pub fn (c $&Conn) write_text(text string) int

Purpose

Operation on $&Conn: write text. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Conn
The Conn instance used by this call (exclusive borrow).
text string
text (string)

Returns

int — a value of this type

($&Conn) write_binary

XuAPI
pub fn (c $&Conn) write_binary(data string) int

Purpose

Operation on $&Conn: write binary. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Conn
The Conn instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&Conn) write_ping

XuAPI
pub fn (c $&Conn) write_ping(data string) int

Purpose

Operation on $&Conn: write ping. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Conn
The Conn instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&Conn) write_pong

XuAPI
pub fn (c $&Conn) write_pong(data string) int

Purpose

Operation on $&Conn: write pong. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Conn
The Conn instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&Conn) close

XuAPI
pub fn (c $&Conn) close(code int, reason string) int

Purpose

Operation on $&Conn: close the resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Conn
The Conn instance used by this call (exclusive borrow).
code int
code (int)
reason string
reason (string)

Returns

int — a value of this type

upgrade

XuAPI
pub fn upgrade(ctx http.Context) Conn

Purpose

upgrade upgrades an HTTP server request to a WebSocket connection. It verifies the required headers, computes the Sec-WebSocket-Accept token, sends the HTTP 101 Switching Protocols response, and returns the Conn. Throws on failure; handle the error or propagate it to the caller.

Parameters

ctx http.Context
ctx (http.Context)

Returns

Conn — a value of this type