websocket example: cleanup and simplify (3/4) (#5923)
parent
635c99e2ed
commit
38aa5d6930
|
@ -1,117 +1,52 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import net.websocket
|
import net.websocket
|
||||||
import eventbus
|
import time
|
||||||
import readline
|
|
||||||
|
|
||||||
const (
|
|
||||||
eb = eventbus.new()
|
|
||||||
)
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// println(sss)
|
//URLs working for testing, reply the same sent messages
|
||||||
/*
|
ws_test('ws://echo.websocket.org')
|
||||||
for sss in 0..10 {
|
ws_test('wss://echo.websocket.org')
|
||||||
mut bm := benchmark.new_benchmark()
|
}
|
||||||
for i in 0..10000 {
|
|
||||||
for a, t in tests {
|
fn ws_test(uri string) {
|
||||||
ss := ws.utf8_validate(t.str, t.len)
|
mut ws := websocket.new(uri)
|
||||||
if !ss {
|
|
||||||
panic("failed")
|
|
||||||
}
|
|
||||||
//println("${a}:${ss}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bm.stop()
|
|
||||||
println( bm.total_message('remarks about the benchmark') )
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
mut ws := websocket.new('ws://localhost:9001/getCaseCount')
|
|
||||||
// ws.nonce_size = 16 // try this, if it does not work with your server
|
|
||||||
// defer { }
|
|
||||||
ws.subscriber.subscribe('on_open', on_open)
|
ws.subscriber.subscribe('on_open', on_open)
|
||||||
ws.subscriber.subscribe('on_message', on_message)
|
ws.subscriber.subscribe('on_message', on_message)
|
||||||
ws.subscriber.subscribe('on_error', on_error)
|
ws.subscriber.subscribe('on_error', on_error)
|
||||||
ws.subscriber.subscribe('on_close', on_close)
|
ws.subscriber.subscribe('on_close', on_close)
|
||||||
// go
|
|
||||||
ws.connect()
|
ws.connect()
|
||||||
ws.read()
|
// Needs another thread, generates an infinite loop for listen
|
||||||
// time.usleep(2000000)
|
go ws.listen()
|
||||||
// go ws.listen()
|
for i := 0; i < 10; i++ {
|
||||||
// term.erase_clear()
|
text := 'a'.repeat(i)
|
||||||
/*
|
println(text)
|
||||||
text := read_line("[client]:")
|
// Send a text to the server
|
||||||
if text == "close" {
|
ws.write(text.str, text.len, .text_frame)
|
||||||
ws.close(1005, "done")
|
// Only for test purposes, to give time to receive message
|
||||||
time.usleep(1000000)
|
time.sleep_ms(100)
|
||||||
exit(0)
|
|
||||||
}
|
}
|
||||||
ws.write(text, .text_frame)
|
// Only for test purposes, to give time to receive message
|
||||||
*/
|
time.sleep_ms(100)
|
||||||
/*
|
|
||||||
time.usleep(1000000)
|
|
||||||
ws.read()
|
|
||||||
*/
|
|
||||||
// ws.close(1005, "done") //
|
|
||||||
// ws.close(1005, "done")
|
|
||||||
// read_line("wait")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_line(text string) string {
|
fn on_open(ws &websocket.Client, x, y voidptr) {
|
||||||
mut r := readline.Readline{}
|
|
||||||
mut output := r.read_line(text + ' ') or {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
output = output.replace('\n', '')
|
|
||||||
if output.len <= 0 {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_open(sender voidptr, ws &websocket.Client, x voidptr) {
|
|
||||||
println('websocket opened.')
|
println('websocket opened.')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(sender voidptr, mut ws websocket.Client, msg &websocket.Message) {
|
fn on_message(ws &websocket.Client, msg &websocket.Message, x voidptr) {
|
||||||
println('Message recieved. Sending it back.')
|
|
||||||
typ := msg.opcode
|
typ := msg.opcode
|
||||||
if typ == .text_frame {
|
if typ == .text_frame {
|
||||||
if ws.uri.ends_with('getCaseCount') {
|
println('Message: ${cstring_to_vstring(msg.payload)}')
|
||||||
num := int(msg.payload)
|
|
||||||
ws.close(1005, 'done')
|
|
||||||
start_tests(mut ws, num)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// println("Message: $msg")
|
|
||||||
ws.write(msg.payload, msg.payload_len, .text_frame)
|
|
||||||
} else {
|
} else {
|
||||||
println('Binary message.')
|
println('Binary message: $msg')
|
||||||
ws.write(msg.payload, msg.payload_len, .binary_frame)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_tests(mut ws websocket.Client, num int) {
|
fn on_close(ws &websocket.Client, x, y voidptr) {
|
||||||
for i := 1; i < num; i++ {
|
|
||||||
println('Running test: ' + i.str())
|
|
||||||
ws.uri = 'ws://localhost:9001/runCase?case=${i.str()}&agent=vws/1.0a'
|
|
||||||
if ws.connect() >= 0 {
|
|
||||||
ws.listen()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println('Done!')
|
|
||||||
ws.uri = 'ws://localhost:9001/updateReports?agent=vws/1.0a'
|
|
||||||
if ws.connect() >= 0 {
|
|
||||||
ws.read()
|
|
||||||
ws.close(1000, 'disconnecting...')
|
|
||||||
}
|
|
||||||
exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_close(sender voidptr, ws &websocket.Client, x voidptr) {
|
|
||||||
println('websocket closed.')
|
println('websocket closed.')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_error(sender voidptr, ws &websocket.Client, x voidptr) {
|
fn on_error(ws &websocket.Client, x, y voidptr) {
|
||||||
println('we have an error.')
|
println('we have an error.')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue