2020-08-22 01:50:38 +03:00
|
|
|
module websocket
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// MessageEventHandler represents a callback on a new message
|
2020-08-22 01:50:38 +03:00
|
|
|
struct MessageEventHandler {
|
2021-01-03 23:10:25 +03:00
|
|
|
handler SocketMessageFn // callback function
|
2020-12-04 03:52:26 +03:00
|
|
|
handler2 SocketMessageFn2 // callback function with reference
|
2021-01-03 23:10:25 +03:00
|
|
|
is_ref bool // true if has a reference object
|
2020-12-04 03:52:26 +03:00
|
|
|
ref voidptr // referenced object
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// ErrorEventHandler represents a callback on error
|
2020-08-22 01:50:38 +03:00
|
|
|
struct ErrorEventHandler {
|
2021-01-03 23:10:25 +03:00
|
|
|
handler SocketErrorFn // callback function
|
2020-12-04 03:52:26 +03:00
|
|
|
handler2 SocketErrorFn2 // callback function with reference
|
2021-01-03 23:10:25 +03:00
|
|
|
is_ref bool // true if has a reference object
|
2020-12-04 03:52:26 +03:00
|
|
|
ref voidptr // referenced object
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// OpenEventHandler represents a callback when connection is opened
|
2020-08-22 01:50:38 +03:00
|
|
|
struct OpenEventHandler {
|
2021-01-03 23:10:25 +03:00
|
|
|
handler SocketOpenFn // callback function
|
2020-12-04 03:52:26 +03:00
|
|
|
handler2 SocketOpenFn2 // callback function with reference
|
2021-01-03 23:10:25 +03:00
|
|
|
is_ref bool // true if has a reference object
|
2020-12-04 03:52:26 +03:00
|
|
|
ref voidptr // referenced object
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// CloseEventHandler represents a callback on a closing event
|
2020-08-22 01:50:38 +03:00
|
|
|
struct CloseEventHandler {
|
2021-01-03 23:10:25 +03:00
|
|
|
handler SocketCloseFn // callback function
|
2020-12-04 03:52:26 +03:00
|
|
|
handler2 SocketCloseFn2 // callback function with reference
|
2021-01-03 23:10:25 +03:00
|
|
|
is_ref bool // true if has a reference object
|
2020-12-04 03:52:26 +03:00
|
|
|
ref voidptr // referenced object
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type AcceptClientFn = fn (mut c ServerClient) ?bool
|
|
|
|
|
|
|
|
pub type SocketMessageFn = fn (mut c Client, msg &Message) ?
|
|
|
|
|
|
|
|
pub type SocketMessageFn2 = fn (mut c Client, msg &Message, v voidptr) ?
|
|
|
|
|
|
|
|
pub type SocketErrorFn = fn (mut c Client, err string) ?
|
|
|
|
|
|
|
|
pub type SocketErrorFn2 = fn (mut c Client, err string, v voidptr) ?
|
|
|
|
|
|
|
|
pub type SocketOpenFn = fn (mut c Client) ?
|
|
|
|
|
|
|
|
pub type SocketOpenFn2 = fn (mut c Client, v voidptr) ?
|
|
|
|
|
|
|
|
pub type SocketCloseFn = fn (mut c Client, code int, reason string) ?
|
|
|
|
|
|
|
|
pub type SocketCloseFn2 = fn (mut c Client, code int, reason string, v voidptr) ?
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_connect registers a callback when client connects to the server
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) on_connect(fun AcceptClientFn) ? {
|
|
|
|
if s.accept_client_callbacks.len > 0 {
|
|
|
|
return error('only one callback can be registered for accept client')
|
|
|
|
}
|
|
|
|
s.accept_client_callbacks << fun
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_message registers a callback on new messages
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) on_message(fun SocketMessageFn) {
|
|
|
|
s.message_callbacks << MessageEventHandler{
|
|
|
|
handler: fun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_message_ref registers a callback on new messages and provides a reference object
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) on_message_ref(fun SocketMessageFn2, ref voidptr) {
|
|
|
|
s.message_callbacks << MessageEventHandler{
|
|
|
|
handler2: fun
|
|
|
|
ref: ref
|
|
|
|
is_ref: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_close registers a callback on closed socket
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) on_close(fun SocketCloseFn) {
|
|
|
|
s.close_callbacks << CloseEventHandler{
|
|
|
|
handler: fun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_close_ref registers a callback on closed socket and provides a reference object
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s Server) on_close_ref(fun SocketCloseFn2, ref voidptr) {
|
|
|
|
s.close_callbacks << CloseEventHandler{
|
|
|
|
handler2: fun
|
|
|
|
ref: ref
|
|
|
|
is_ref: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_message registers a callback on new messages
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_message(fun SocketMessageFn) {
|
|
|
|
ws.message_callbacks << MessageEventHandler{
|
|
|
|
handler: fun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_message_ref registers a callback on new messages and provides a reference object
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_message_ref(fun SocketMessageFn2, ref voidptr) {
|
|
|
|
ws.message_callbacks << MessageEventHandler{
|
|
|
|
handler2: fun
|
|
|
|
ref: ref
|
|
|
|
is_ref: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_error registers a callback on errors
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_error(fun SocketErrorFn) {
|
|
|
|
ws.error_callbacks << ErrorEventHandler{
|
|
|
|
handler: fun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_error_ref registers a callback on errors and provides a reference object
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_error_ref(fun SocketErrorFn2, ref voidptr) {
|
|
|
|
ws.error_callbacks << ErrorEventHandler{
|
|
|
|
handler2: fun
|
|
|
|
ref: ref
|
|
|
|
is_ref: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_open registers a callback on successful opening the websocket
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_open(fun SocketOpenFn) {
|
|
|
|
ws.open_callbacks << OpenEventHandler{
|
|
|
|
handler: fun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_open_ref registers a callback on successful opening the websocket
|
|
|
|
// and provides a reference object
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_open_ref(fun SocketOpenFn2, ref voidptr) {
|
|
|
|
ws.open_callbacks << OpenEventHandler{
|
|
|
|
handler2: fun
|
|
|
|
ref: ref
|
|
|
|
is_ref: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_close registers a callback on closed socket
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_close(fun SocketCloseFn) {
|
|
|
|
ws.close_callbacks << CloseEventHandler{
|
|
|
|
handler: fun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// on_close_ref registers a callback on closed socket and provides a reference object
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) on_close_ref(fun SocketCloseFn2, ref voidptr) {
|
|
|
|
ws.close_callbacks << CloseEventHandler{
|
|
|
|
handler2: fun
|
|
|
|
ref: ref
|
|
|
|
is_ref: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// send_connect_event invokes the on_connect callback
|
2020-12-02 06:02:53 +03:00
|
|
|
fn (mut s Server) send_connect_event(mut c ServerClient) ?bool {
|
|
|
|
if s.accept_client_callbacks.len == 0 {
|
|
|
|
// If no callback all client will be accepted
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
fun := s.accept_client_callbacks[0]
|
|
|
|
res := fun(mut c) ?
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// send_message_event invokes the on_message callback
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) send_message_event(msg &Message) {
|
|
|
|
ws.debug_log('sending on_message event')
|
|
|
|
for ev_handler in ws.message_callbacks {
|
|
|
|
if !ev_handler.is_ref {
|
|
|
|
ev_handler.handler(ws, msg)
|
|
|
|
} else {
|
|
|
|
ev_handler.handler2(ws, msg, ev_handler.ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// send_error_event invokes the on_error callback
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) send_error_event(err string) {
|
|
|
|
ws.debug_log('sending on_error event')
|
|
|
|
for ev_handler in ws.error_callbacks {
|
|
|
|
if !ev_handler.is_ref {
|
|
|
|
ev_handler.handler(mut ws, err)
|
|
|
|
} else {
|
|
|
|
ev_handler.handler2(mut ws, err, ev_handler.ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// send_close_event invokes the on_close callback
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) send_close_event(code int, reason string) {
|
|
|
|
ws.debug_log('sending on_close event')
|
|
|
|
for ev_handler in ws.close_callbacks {
|
|
|
|
if !ev_handler.is_ref {
|
|
|
|
ev_handler.handler(mut ws, code, reason)
|
|
|
|
} else {
|
|
|
|
ev_handler.handler2(mut ws, code, reason, ev_handler.ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// send_open_event invokes the on_open callback
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) send_open_event() {
|
|
|
|
ws.debug_log('sending on_open event')
|
|
|
|
for ev_handler in ws.open_callbacks {
|
|
|
|
if !ev_handler.is_ref {
|
|
|
|
ev_handler.handler(mut ws)
|
|
|
|
} else {
|
|
|
|
ev_handler.handler2(mut ws, ev_handler.ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|