Module net.websocket stdlib

net.websocket
Version:
0.3.3
License:
MIT
Dependencies from vmod:
0
Imports:
13
Imported by:
0
Repository:
OS-specific
Show selected OS-specific symbols.
Backend-specific
Show selected Backend-specific symbols.

Dependencies defined in v.mod

This section is empty.

Imported by

This section is empty.

Aliases

This section is empty.

Constants

This section is empty.

Sum types

This section is empty.

Functions

This section is empty.

Structs

#struct Client

pub 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 new_client

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

new_client instance a new websocket client

#fn (&Client) on_message

fn (mut ws &Client) on_message(fun fn (mut &Client, &Message) !)

on_message registers a callback on new messages

#fn (&Client) on_message_ref

fn (mut ws &Client) on_message_ref(fun fn (mut &Client, &Message, voidptr) !, ref voidptr)

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

#fn (&Client) on_error

fn (mut ws &Client) on_error(fun fn (mut &Client, string) !)

on_error registers a callback on errors

#fn (&Client) on_error_ref

fn (mut ws &Client) on_error_ref(fun fn (mut &Client, string, voidptr) !, ref voidptr)

on_error_ref registers a callback on errors and provides a reference object

#fn (&Client) on_open

fn (mut ws &Client) on_open(fun fn (mut &Client) !)

on_open registers a callback on successful opening the websocket

#fn (&Client) on_open_ref

fn (mut ws &Client) on_open_ref(fun fn (mut &Client, voidptr) !, ref voidptr)

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

#fn (&Client) on_close

fn (mut ws &Client) on_close(fun fn (mut &Client, int, string) !)

on_close registers a callback on closed socket

#fn (&Client) on_close_ref

fn (mut ws &Client) on_close_ref(fun fn (mut &Client, int, string, voidptr) !, ref voidptr)

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

#fn (&Client) send_message_event

fn (mut ws &Client) send_message_event(msg &Message)

send_message_event invokes the on_message callback

#fn (&Client) send_error_event

fn (mut ws &Client) send_error_event(error string)

send_error_event invokes the on_error callback

#fn (&Client) send_close_event

fn (mut ws &Client) send_close_event(code int, reason string)

send_close_event invokes the on_close callback

#fn (&Client) send_open_event

fn (mut ws &Client) send_open_event()

send_open_event invokes the on_open callback

#fn (&Client) handshake

fn (mut ws &Client) handshake() !

handshake manages the websocket handshake process

#fn (&Client) read_handshake_str

fn (mut ws &Client) read_handshake_str() !string

read_handshake_str returns the handshake response

#fn (&Client) read_handshake

fn (mut ws &Client) read_handshake(seckey string) !

read_handshake reads the handshake result and check if valid

#fn (&Client) check_handshake_response

fn (mut ws &Client) check_handshake_response(handshake_response string, seckey string) !

check_handshake_response checks the response from handshake and returns the response and secure key provided by the websocket client

#fn (&Client) socket_read

fn (mut ws &Client) socket_read(mut buffer &[]u8) !int

socket_read reads from socket into the provided buffer

#fn (&Client) socket_read_ptr

fn (mut ws &Client) socket_read_ptr(buf_ptr &u8, len int) !int

socket_read reads from socket into the provided byte pointer and length

#fn (&Client) socket_write

fn (mut ws &Client) socket_write(bytes []u8) !int

socket_write writes the provided byte array to the socket

#fn (&Client) shutdown_socket

fn (mut ws &Client) shutdown_socket() !

shutdown_socket shuts down the socket properly when connection is closed

#fn (&Client) dial_socket

fn (mut ws &Client) dial_socket() !&net.TcpConn

dial_socket connects tcp socket and initializes default configurations

#fn (&Client) validate_frame

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

validate_client validates client frame rules from RFC6455

#fn (&Client) read_payload

fn (mut ws &Client) read_payload(frame &Frame) ![]u8

read_payload reads the message payload from the socket

#fn (&Client) validate_utf_8

fn (mut ws &Client) validate_utf_8(opcode OPCode, payload []u8) !

validate_utf_8 validates payload for valid utf8 encoding - Future implementation needs to support fail fast utf errors for strict autobahn conformance

#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) payload_from_fragments

fn (ws Client) payload_from_fragments(fin_payload []u8) ![]u8

payload_from_fragments returs the whole paylaod from fragmented message

#fn (Client) opcode_from_fragments

fn (ws Client) opcode_from_fragments() OPCode

opcode_from_fragments returns the opcode for message from the first fragment sent

#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) connect

fn (mut ws &Client) connect() !

connect connects to remote websocket server

#fn (&Client) listen

fn (mut ws &Client) listen() !

listen listens and processes incoming messages

#fn (&Client) manage_clean_close

fn (mut ws &Client) manage_clean_close()

manage_clean_close closes connection in a clean websocket way

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

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

write writes a byte array with a websocket messagetype 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

#fn (&Client) close

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

close closes the websocket connection

#fn (&Client) send_control_frame

fn (mut ws &Client) send_control_frame(code OPCode, frame_typ string, payload []u8) !

send_control_frame sends a control frame to the server

#fn (&Client) set_state

fn (mut ws &Client) set_state(state State)

set_state sets current state of the websocket connection

#fn (&Client) assert_not_connected

fn (ws &Client) assert_not_connected() !

assert_not_connected returns error if the connection is not connected

#fn (&Client) reset_state

fn (mut ws &Client) reset_state() !

reset_state resets the websocket and initialize default settings

#fn (&Client) debug_log

fn (mut ws &Client) debug_log(text string)

debug_log handles debug logging output for client and server

#fn (&Client) free

fn (c &Client) free()

free handles manual free memory of Client struct

#struct Message

pub 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 ClientOpt

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

#struct Server

pub 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 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

#fn (&Server) on_connect

fn (mut s &Server) on_connect(fun fn (mut &ServerClient) !bool) !

on_connect registers a callback when client connects to the server

#fn (&Server) on_message

fn (mut s &Server) on_message(fun fn (mut &Client, &Message) !)

on_message registers a callback on new messages

#fn (&Server) on_message_ref

fn (mut s &Server) on_message_ref(fun fn (mut &Client, &Message, voidptr) !, ref voidptr)

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

#fn (&Server) on_close

fn (mut s &Server) on_close(fun fn (mut &Client, int, string) !)

on_close registers a callback on closed socket

#fn (&Server) on_close_ref

fn (mut s &Server) on_close_ref(fun fn (mut &Client, int, string, voidptr) !, ref voidptr)

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

#fn (&Server) send_connect_event

fn (mut s &Server) send_connect_event(mut c &ServerClient) !bool

send_connect_event invokes the on_connect callback

#fn (&Server) handle_server_handshake

fn (mut s &Server) handle_server_handshake(mut c &Client) !(string, &ServerClient)

handle_server_handshake manages websocket server handshake process

#fn (&Server) parse_client_handshake

fn (mut s &Server) parse_client_handshake(client_handshake string, mut c &Client) !(string, &ServerClient)

parse_client_handshake parses result from handshake process

#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

#fn (&Server) listen

fn (mut s &Server) listen() !

listen start listen and process to incoming connections from websocket clients

#fn (&Server) close

fn (mut s &Server) close()

Close closes server (not implemented yet)

#fn (&Server) handle_ping

fn (mut s &Server) handle_ping()

handle_ping sends ping to all clients every set interval

#fn (&Server) serve_client

fn (mut s &Server) serve_client(mut c &Client) !

serve_client accepts incoming connection and sets up the callbacks

#fn (&Server) setup_callbacks

fn (mut s &Server) setup_callbacks(mut sc &ServerClient)

setup_callbacks initialize all callback functions

#fn (&Server) accept_new_client

fn (mut s &Server) accept_new_client() !&Client

accept_new_client creates a new client instance for client that connects to the socket

#fn (&Server) set_state

fn (mut s &Server) set_state(state State)

set_state sets current state in a thread safe way

#fn (&Server) free

fn (mut s &Server) free()

free manages manual free of memory for Server instance

#struct ServerClient

pub 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

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

Interfaces

This section is empty.

Enums

#enum State

pub enum State {
	connecting = 0
	open
	closing
	closed
}

State represents the state of the websocket connection.

#enum OPCode

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

OPCode represents the supported websocket frame types