net.websocket #

fn new_client #

fn new_client(address string) ?&Client

new_client instance a new websocket client

fn new_server #

fn new_server(family net.AddrFamily, port int, route string) &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          &openssl.SSLConn
	flags             []Flag
	fragments         []Fragment
	message_callbacks []MessageEventHandler
	error_callbacks   []ErrorEventHandler
	open_callbacks    []OpenEventHandler
	close_callbacks   []CloseEventHandler
pub:
	is_ssl bool
	uri    Uri
	id     string
pub mut:
	header            http.Header
	conn              &net.TcpConn
	nonce_size        int = 16
	panic_on_callback bool
	state             State
	logger            &log.Log
	resource_name     string
	last_pong_ut      u64
}

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 []byte, 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 &byte, payload_len int, code OPCode) ?int

write_ptr writes len bytes provided a byteptr with a websocket messagetype

fn (Client) write_str #

deprecated
fn (mut ws Client) write_str(str string) ?int

write_string, writes a string with a websocket texttype to socket

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 Message #

struct Message {
pub:
	opcode  OPCode
	payload []byte
}

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.Log
	ls                      &net.TcpListener
	accept_client_callbacks []AcceptClientFn
	message_callbacks       []MessageEventHandler
	close_callbacks         []CloseEventHandler
pub:
	family net.AddrFamily = .ip
	port   int
	is_ssl bool
pub mut:
	clients       map[string]&ServerClient
	ping_interval int = 30
	state         State
}

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