websocket: fmt example + minor fixes

pull/4301/head
Alexander Medvednikov 2020-04-08 21:21:58 +02:00
parent 6f8f8d7b1b
commit 3521b7ff89
2 changed files with 62 additions and 54 deletions

View File

@ -12,13 +12,15 @@ import (
const ( const (
eb = eventbus.new() eb = eventbus.new()
) )
#flag -I $PWD #flag -I $PWD
#include "utf8.h" #include "utf8.h"
fn C.utf8_validate_str() bool fn C.utf8_validate_str() bool
fn main(){
//println(sss) fn main() {
/* for sss in 0..10 { // println(sss)
/*
for sss in 0..10 {
mut bm := benchmark.new_benchmark() mut bm := benchmark.new_benchmark()
for i in 0..10000 { for i in 0..10000 {
for a, t in tests { for a, t in tests {
@ -31,94 +33,97 @@ fn main(){
} }
bm.stop() bm.stop()
println( bm.total_message('remarks about the benchmark') ) println( bm.total_message('remarks about the benchmark') )
} */ }
mut ws := websocket.new("ws://localhost:9001/getCaseCount") */
//defer { } mut ws := websocket.new('ws://localhost:9001/getCaseCount')
ws.subscriber.subscribe("on_open", on_open) // defer { }
ws.subscriber.subscribe("on_message", on_message) ws.subscriber.subscribe('on_open', on_open)
ws.subscriber.subscribe("on_error", on_error) ws.subscriber.subscribe('on_message', on_message)
ws.subscriber.subscribe("on_close", on_close) ws.subscriber.subscribe('on_error', on_error)
//go ws.subscriber.subscribe('on_close', on_close)
// go
ws.connect() ws.connect()
ws.read() ws.read()
//time.usleep(2000000) // time.usleep(2000000)
//go ws.listen() // go ws.listen()
//term.erase_clear() // term.erase_clear()
/* text := read_line("[client]:") /*
text := read_line("[client]:")
if text == "close" { if text == "close" {
ws.close(1005, "done") ws.close(1005, "done")
time.usleep(1000000) time.usleep(1000000)
exit(0) exit(0)
} }
ws.write(text, .text_frame) */ ws.write(text, .text_frame)
/* time.usleep(1000000) */
ws.read() */
//ws.close(1005, "done")
/* /*
*/ time.usleep(1000000)
//ws.close(1005, "done") ws.read()
//read_line("wait") */
// ws.close(1005, "done") //
// ws.close(1005, "done")
// read_line("wait")
} }
fn read_line(text string) string { fn read_line(text string) string {
mut r := readline.Readline{} mut r := readline.Readline{}
mut output := r.read_line(text + " ") or { mut output := r.read_line(text + ' ') or {
panic(err) panic(err)
} }
output = output.replace("\n","") output = output.replace('\n', '')
if output.len <= 0 { if output.len <= 0 {
return "" return ''
} }
return output return output
} }
fn on_open(params eventbus.Params){ fn on_open(params eventbus.Params) {
println("websocket opened.") println('websocket opened.')
} }
fn on_message(params eventbus.Params){ fn on_message(params eventbus.Params) {
println("Message recieved. Sending it back.") println('Message recieved. Sending it back.')
typ := params.get_string("type") typ := params.get_string('type')
len := params.get_int("len") len := params.get_int('len')
mut ws := params.get_caller(ws.Client{}) mut ws := params.get_caller(websocket.Client{})
if typ == "string" { if typ == 'string' {
message := params.get_string("payload") message := params.get_string('payload')
if ws.uri.ends_with("getCaseCount") { if ws.uri.ends_with('getCaseCount') {
num := message.int() num := message.int()
ws.close(1005, "done") ws.close(1005, 'done')
start_tests(mut ws, num) start_tests(mut ws, num)
return return
} }
//println("Message: " + message) // println("Message: " + message)
ws.write(message.str, len, .text_frame) ws.write(message.str, len, .text_frame)
} else { } else {
println("Binary message.") println('Binary message.')
message := params.get_raw("payload") message := params.get_raw('payload')
ws.write(message, len, .binary_frame) ws.write(message, len, .binary_frame)
} }
} }
fn start_tests(ws mut ws.Client, num int) { fn start_tests(ws mut websocket.Client, num int) {
for i := 1; i < num; i++ { for i := 1; i < num; i++ {
println("Running test: " + i.str()) println('Running test: ' + i.str())
ws.uri = "ws://localhost:9001/runCase?case=${i.str()}&agent=vws/1.0a" ws.uri = 'ws://localhost:9001/runCase?case=${i.str()}&agent=vws/1.0a'
if ws.connect() >= 0 { if ws.connect() >= 0 {
ws.listen() ws.listen()
} }
} }
println("Done!") println('Done!')
ws.uri = "ws://localhost:9001/updateReports?agent=vws/1.0a" ws.uri = 'ws://localhost:9001/updateReports?agent=vws/1.0a'
if ws.connect() >= 0 { if ws.connect() >= 0 {
ws.read() ws.read()
ws.close(1000, "disconnecting...") ws.close(1000, 'disconnecting...')
} }
exit(0) exit(0)
} }
fn on_close(params eventbus.Params){ fn on_close(params eventbus.Params) {
println("websocket closed.") println('websocket closed.')
} }
fn on_error(params eventbus.Params){ fn on_error(params eventbus.Params) {
println("we have an error.") println('we have an error.')
} }

View File

@ -281,6 +281,9 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
ast.GotoStmt { ast.GotoStmt {
f.writeln('goto $it.name') f.writeln('goto $it.name')
} }
ast.HashStmt {
f.writeln('#$it.val')
}
ast.Import { ast.Import {
f.imports(f.file.imports) f.imports(f.file.imports)
} }