websocket: eventbus and other cleanup
parent
b40fdd9089
commit
6f8f8d7b1b
|
@ -4,14 +4,15 @@ Originally located at [thecodrr/vws](https://github.com/thecodrr/vws) (contains
|
||||||
|
|
||||||
**This is still work-in-progress!**
|
**This is still work-in-progress!**
|
||||||
|
|
||||||
Heavily inspired (and used **very** liberally) from [cwebsockets](https://github.com/jeremyhahn/cwebsocket).
|
Heavily inspired from [cwebsockets](https://github.com/jeremyhahn/cwebsocket).
|
||||||
|
|
||||||
The websockets library itself is ready and working (passes all tests of AutoBahn). What's left:
|
The websockets library itself is ready and working (passes all tests of AutoBahn). What's left:
|
||||||
|
|
||||||
1. It needs to be updated and made to run with latest V.
|
1. It needs to be updated and made to run with latest V.
|
||||||
2. No Windows Support (SSL issues)
|
2. No Windows Support (SSL issues)
|
||||||
3. No proper AutoBahn test client (a prototype is in the main.v but nothing clean and neat).
|
3. No proper AutoBahn test client (a prototype is in the main.v but nothing proper).
|
||||||
4. No Websocket Server.
|
4. No Websocket Server.
|
||||||
|
5. Remove the `logger` and move to `log`
|
||||||
|
|
||||||
## What's needed for Windows support:
|
## What's needed for Windows support:
|
||||||
|
|
||||||
|
|
|
@ -1,40 +1,21 @@
|
||||||
module websocket
|
module websocket
|
||||||
|
|
||||||
import (
|
fn (ws &Client) send_message_event(msg Message) {
|
||||||
eventbus
|
ws.eb.publish('on_message', ws, msg)
|
||||||
)
|
l.d('sending on_message event')
|
||||||
|
|
||||||
fn (ws &Client) send_message_event(msg Message){
|
|
||||||
mut params := eventbus.Params{}
|
|
||||||
mut typ := ""
|
|
||||||
if msg.opcode == .text_frame {
|
|
||||||
params.put_string("payload", string(byteptr(msg.payload)))
|
|
||||||
typ = 'string'
|
|
||||||
} else if msg.opcode == .binary_frame {
|
|
||||||
params.put_custom("payload", "binary", msg.payload)
|
|
||||||
typ = 'binary'
|
|
||||||
}
|
|
||||||
params.put_string("type", typ)
|
|
||||||
params.put_int("len", msg.payload_len)
|
|
||||||
ws.eb.publish("on_message", params, ws)
|
|
||||||
l.d("sending on_message event")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (ws &Client) send_error_event(err string) {
|
fn (ws &Client) send_error_event(err string) {
|
||||||
mut params := eventbus.Params{}
|
ws.eb.publish('on_error', ws, err)
|
||||||
params.put_string("error", err)
|
l.d('sending on_error event')
|
||||||
ws.eb.publish("on_error", params, ws)
|
|
||||||
l.d("sending on_error event")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (ws &Client) send_close_event() {
|
fn (ws &Client) send_close_event() {
|
||||||
params := eventbus.Params{}
|
ws.eb.publish('on_close', ws, voidptr(0))
|
||||||
ws.eb.publish("on_close", params, ws)
|
l.d('sending on_close event')
|
||||||
l.d("sending on_close event")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (ws &Client) send_open_event() {
|
fn (ws &Client) send_open_event() {
|
||||||
params := eventbus.Params{}
|
ws.eb.publish('on_open', ws, voidptr(0))
|
||||||
ws.eb.publish("on_open", params, ws)
|
l.d('sending on_open event')
|
||||||
l.d("sending on_open event")
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -40,7 +40,8 @@ struct Fragment {
|
||||||
code OPCode
|
code OPCode
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Message {
|
pub struct Message {
|
||||||
|
pub:
|
||||||
opcode OPCode
|
opcode OPCode
|
||||||
payload voidptr
|
payload voidptr
|
||||||
payload_len int
|
payload_len int
|
||||||
|
@ -117,12 +118,18 @@ fn (ws &Client) parse_uri() &Uri {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ws mut Client) connect() int {
|
pub fn (ws mut Client) connect() int {
|
||||||
if ws.state == .connected {
|
match ws.state {
|
||||||
l.f("connect: websocket already connected")
|
.connected {
|
||||||
} else if ws.state == .connecting {
|
l.f("connect: websocket already connected")
|
||||||
l.f("connect: websocket already connecting")
|
}
|
||||||
} else if ws.state == .open {
|
.connecting {
|
||||||
l.f("connect: websocket already open")
|
l.f("connect: websocket already connecting")
|
||||||
|
}
|
||||||
|
.open {
|
||||||
|
l.f("connect: websocket already open")
|
||||||
|
} else {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.lock.lock()
|
ws.lock.lock()
|
||||||
|
|
Loading…
Reference in New Issue