Skip to content

net.websocket #

fn new_client #

fn new_client(address string, opt ClientOpt) ?&Client

new_client instance a new websocket client

fn new_server #

fn new_server(family net.AddrFamily, port int, route string, opt ServerOpt) &Server

new_server instance a new websocket server on provided port and route

type AcceptClientFn #

type AcceptClientFn = fn (mut c ServerClient) ?bool

type SocketCloseFn #

type SocketCloseFn = fn (mut c Client, code int, reason string) ?

type SocketCloseFn2 #

type SocketCloseFn2 = fn (mut c Client, code int, reason string, v voidptr) ?

type SocketErrorFn #

type SocketErrorFn = fn (mut c Client, err string) ?

type SocketErrorFn2 #

type SocketErrorFn2 = fn (mut c Client, err string, v voidptr) ?

type SocketMessageFn #

type SocketMessageFn = fn (mut c Client, msg &Message) ?

type SocketMessageFn2 #

type SocketMessageFn2 = fn (mut c Client, msg &Message, v voidptr) ?

type SocketOpenFn #

type SocketOpenFn = fn (mut c Client) ?

type SocketOpenFn2 #

type SocketOpenFn2 = fn (mut c Client, v voidptr) ?

fn (Uri) str #

fn (u Uri) str() string

str returns the string representation of the Uri

enum OPCode #

enum OPCode {
	continuation = 0x00
	text_frame = 0x01
	binary_frame = 0x02
	close = 0x08
	ping = 0x09
	pong = 0x0A
}

OPCode represents the supported websocket frame types

enum State #

enum State {
	connecting = 0
	open
	closing
	closed
}

State represents the state of the websocket connection.

struct Client #

struct Client {
	is_server bool
mut:
	ssl_conn          &ssl.SSLConn = unsafe { nil } // secure connection used when wss is used
	flags             []Flag       // flags used in handshake
	fragments         []Fragment   // current fragments
	message_callbacks []MessageEventHandler // all callbacks on_message
	error_callbacks   []ErrorEventHandler   // all callbacks on_error
	open_callbacks    []OpenEventHandler    // all callbacks on_open
	close_callbacks   []CloseEventHandler   // all callbacks on_close
pub:
	is_ssl        bool   // true if secure socket is used
	uri           Uri    // uri of current connection
	id            string // unique id of client
	read_timeout  i64
	write_timeout i64
pub mut:
	header            http.Header  // headers that will be passed when connecting
	conn              &net.TcpConn = unsafe { nil } // underlying TCP socket connection
	nonce_size        int = 16 // size of nounce used for masking
	panic_on_callback bool  // set to true of callbacks can panic
	state             State // current state of connection
// logger used to log messages
	logger &log.Logger = &log.Logger(&log.Log{
	level: .info
})
	resource_name string // name of current resource
	last_pong_ut  i64    // last time in unix time we got a pong message
}

Client represents websocket client

fn (Client) close #

fn (mut ws Client) close(code int, message string) ?

close closes the websocket connection

fn (Client) connect #

fn (mut ws Client) connect() ?

connect connects to remote websocket server

fn (Client) free #

fn (c &Client) free()

free handles manual free memory of Client struct

fn (Client) listen #

fn (mut ws Client) listen() ?

listen listens and processes incoming messages

fn (Client) on_close #

fn (mut ws Client) on_close(fun SocketCloseFn)

on_close registers a callback on closed socket

fn (Client) on_close_ref #

fn (mut ws Client) on_close_ref(fun SocketCloseFn2, ref voidptr)

on_close_ref registers a callback on closed socket and provides a reference object

fn (Client) on_error #

fn (mut ws Client) on_error(fun SocketErrorFn)

on_error registers a callback on errors

fn (Client) on_error_ref #

fn (mut ws Client) on_error_ref(fun SocketErrorFn2, ref voidptr)

on_error_ref registers a callback on errors and provides a reference object

fn (Client) on_message #

fn (mut ws Client) on_message(fun SocketMessageFn)

on_message registers a callback on new messages

fn (Client) on_message_ref #

fn (mut ws Client) on_message_ref(fun SocketMessageFn2, ref voidptr)

on_message_ref registers a callback on new messages and provides a reference object

fn (Client) on_open #

fn (mut ws Client) on_open(fun SocketOpenFn)

on_open registers a callback on successful opening the websocket

fn (Client) on_open_ref #

fn (mut ws Client) on_open_ref(fun SocketOpenFn2, ref voidptr)

on_open_ref registers a callback on successful opening the websocket and provides a reference object

fn (Client) parse_frame_header #

fn (mut ws Client) parse_frame_header() ?Frame

parse_frame_header parses next message by decoding the incoming frames

fn (Client) ping #

fn (mut ws Client) ping() ?

ping sends ping message to server

fn (Client) pong #

fn (mut ws Client) pong() ?

pong sends pong message to server,

fn (Client) read_next_message #

fn (mut ws Client) read_next_message() ?Message

read_next_message reads 1 to n frames to compose a message

fn (Client) validate_frame #

fn (mut ws Client) validate_frame(frame &Frame) ?

validate_client validates client frame rules from RFC6455

fn (Client) write #

fn (mut ws Client) write(bytes []u8, code OPCode) ?int

write writes a byte array with a websocket messagetype to socket

fn (Client) write_ptr #

fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) ?int

write_ptr writes len bytes provided a byteptr with a websocket messagetype

fn (Client) write_string #

fn (mut ws Client) write_string(str string) ?int

write_str, writes a string with a websocket texttype to socket

struct ClientOpt #

struct ClientOpt {
	read_timeout  i64 = 30 * time.second
	write_timeout i64 = 30 * time.second
	logger        &log.Logger = &log.Logger(&log.Log{
	level: .info
})
}

struct Message #

struct Message {
pub:
	opcode  OPCode // websocket frame type of this message
	payload []u8   // payload of the message
}

Message represents a whole message combined from 1 to n frames

fn (Message) free #

fn (m &Message) free()

free handles manual free memory of Message struct

struct Server #

struct Server {
mut:
	logger &log.Logger = &log.Logger(&log.Log{
	level: .info
})
	ls                      &net.TcpListener = unsafe { nil } // listener used to get incoming connection to socket
	accept_client_callbacks []AcceptClientFn      // accept client callback functions
	message_callbacks       []MessageEventHandler // new message callback functions
	close_callbacks         []CloseEventHandler   // close message callback functions
pub:
	family net.AddrFamily = .ip
	port   int  // port used as listen to incoming connections
	is_ssl bool // true if secure connection (not supported yet on server)
pub mut:
	clients       map[string]&ServerClient // clients connected to this server
	ping_interval int = 30 // interval for sending ping to clients (seconds)
	state         State // current state of connection
}

Server represents a websocket server connection

fn (Server) free #

fn (mut s Server) free()

free manages manual free of memory for Server instance

fn (Server) listen #

fn (mut s Server) listen() ?

listen start listen and process to incoming connections from websocket clients

fn (Server) on_close #

fn (mut s Server) on_close(fun SocketCloseFn)

on_close registers a callback on closed socket

fn (Server) on_close_ref #

fn (mut s Server) on_close_ref(fun SocketCloseFn2, ref voidptr)

on_close_ref registers a callback on closed socket and provides a reference object

fn (Server) on_connect #

fn (mut s Server) on_connect(fun AcceptClientFn) ?

on_connect registers a callback when client connects to the server

fn (Server) on_message #

fn (mut s Server) on_message(fun SocketMessageFn)

on_message registers a callback on new messages

fn (Server) on_message_ref #

fn (mut s Server) on_message_ref(fun SocketMessageFn2, ref voidptr)

on_message_ref registers a callback on new messages and provides a reference object

fn (Server) set_ping_interval #

fn (mut s Server) set_ping_interval(seconds int)

set_ping_interval sets the interval that the server will send ping messages to clients

struct ServerClient #

struct ServerClient {
pub:
	resource_name string // resource that the client access
	client_key    string // unique key of client
pub mut:
	server &Server = unsafe { nil }
	client &Client = unsafe { nil }
}

ServerClient represents a connected client

struct ServerOpt #

struct ServerOpt {
	logger &log.Logger = &log.Logger(&log.Log{
	level: .info
})
}