x.websocket: server broadcast plus examples (#7922)

pull/7925/head
Tomas Hellström 2021-01-06 15:43:54 +01:00 committed by GitHub
parent 42e60b8e27
commit f9a873736e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 112 additions and 5 deletions

View File

@ -118,6 +118,8 @@ pub fn new_test_session(_vargs string) TestSession {
$if windows {
skip_files << 'examples/database/mysql.v'
skip_files << 'examples/x/websocket/ping.v' // requires OpenSSL
skip_files << 'examples/x/websocket/client-server/client.v' // requires OpenSSL
skip_files << 'examples/x/websocket/client-server/server.v' // requires OpenSSL
}
if github_job != 'ubuntu-tcc' {
skip_files << 'examples/wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases

View File

@ -0,0 +1,60 @@
module main
import os
import x.websocket
import term
// This client should be compiled an run in different konsoles
// it connects to the server who will broadcast your messages
// to all other connected clients
fn main() {
mut ws := start_client() ?
println(term.green('client $ws.id ready'))
println('Write message and enter to send...')
for {
line := os.get_line()
if line == '' {
break
}
ws.write_str(line)
}
ws.close(1000, 'normal') or {
println(term.red('panicing $err'))
}
unsafe {
ws.free()
}
}
fn start_client() ?&websocket.Client {
mut ws := websocket.new_client('ws://localhost:30000')?
// mut ws := websocket.new_client('wss://echo.websocket.org:443')?
// use on_open_ref if you want to send any reference object
ws.on_open(fn (mut ws websocket.Client) ? {
println(term.green('websocket connected to the server and ready to send messages...'))
})
// use on_error_ref if you want to send any reference object
ws.on_error(fn (mut ws websocket.Client, err string) ? {
println(term.red('error: $err'))
})
// use on_close_ref if you want to send any reference object
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
println(term.green('the connection to the server successfully closed'))
})
// on new messages from other clients, display them in blue text
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
if msg.payload.len > 0 {
message := msg.payload.bytestr()
println(term.blue('$message'))
}
})
ws.connect() or {
println(term.red('error on connect: $err'))
}
go ws.listen() or {
println(term.red('error on listen $err'))
}
return ws
}

View File

@ -0,0 +1,48 @@
module main
import os
import x.websocket
import term
// this server accepts client connections and broadcast all messages to other connected clients
fn main() {
go start_server()
println('press enter to quit...')
os.get_line()
}
fn start_server() ? {
mut s := websocket.new_server(30000, '')
// Make that in execution test time give time to execute at least one time
s.ping_interval = 100
s.on_connect(fn (mut s websocket.ServerClient) ?bool {
// Here you can look att the client info and accept or not accept
// just returning a true/false
if s.resource_name != '/' {
return false
}
return true
})?
// on_message_ref, broadcast all incoming messages to all clients except the one sent it
s.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut m websocket.Server) ? {
for _, cli in m.clients {
mut c := cli
if c.client.state == .open && c.client.id != ws.id {
c.client.write(msg.payload, websocket.OPCode.text_frame) or {
panic(err)
}
}
}
}, s)
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
println(term.green('client ($ws.id) closed connection'))
})
s.listen() or {
println(term.red('error on server listen: $err'))
}
unsafe {
s.free()
}
}

View File

@ -333,11 +333,9 @@ pub fn (mut ws Client) close(code int, message string) ? {
close_frame[i + 2] = message[i]
}
ws.send_control_frame(.close, 'CLOSE', close_frame) ?
ws.send_close_event(code, message)
unsafe { close_frame.free() }
} else {
ws.send_control_frame(.close, 'CLOSE', []) ?
ws.send_close_event(code, '')
}
ws.fragments = []
}

View File

@ -10,7 +10,6 @@ import rand
// Server represents a websocket server connection
pub struct Server {
mut:
clients map[string]&ServerClient // clients connected to this server
logger &log.Log // logger used to log
ls net.TcpListener // listener used to get incoming connection to socket
accept_client_callbacks []AcceptClientFn // accept client callback functions
@ -20,8 +19,8 @@ pub:
port int // port used as listen to incoming connections
is_ssl bool // true if secure connection (not supported yet on server)
pub mut:
ping_interval int = 30
// interval for sending ping to clients (seconds)
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
}