mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: change optional to result of io (#16075)
This commit is contained in:
@@ -8,7 +8,7 @@ import term
|
||||
// it connects to the server who will broadcast your messages
|
||||
// to all other connected clients
|
||||
fn main() {
|
||||
mut ws := start_client()?
|
||||
mut ws := start_client()!
|
||||
println(term.green('client $ws.id ready'))
|
||||
println('Write message and enter to send...')
|
||||
for {
|
||||
@@ -16,7 +16,7 @@ fn main() {
|
||||
if line == '' {
|
||||
break
|
||||
}
|
||||
ws.write_string(line)?
|
||||
ws.write_string(line)!
|
||||
}
|
||||
ws.close(1000, 'normal') or { println(term.red('panicing $err')) }
|
||||
unsafe {
|
||||
@@ -24,23 +24,23 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn start_client() ?&websocket.Client {
|
||||
mut ws := websocket.new_client('ws://localhost:30000')?
|
||||
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) ? {
|
||||
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) ? {
|
||||
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) ? {
|
||||
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) ? {
|
||||
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
|
||||
if msg.payload.len > 0 {
|
||||
message := msg.payload.bytestr()
|
||||
println(term.blue('$message'))
|
||||
|
@@ -6,24 +6,24 @@ import term
|
||||
// this server accepts client connections and broadcast all messages to other connected clients
|
||||
fn main() {
|
||||
println('press ctrl-c to quit...')
|
||||
start_server()?
|
||||
start_server()!
|
||||
}
|
||||
|
||||
fn start_server() ? {
|
||||
fn start_server() ! {
|
||||
mut s := websocket.new_server(.ip6, 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 {
|
||||
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) ? {
|
||||
s.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut m websocket.Server) ! {
|
||||
// for _, cli in m.clients {
|
||||
for i, _ in m.clients {
|
||||
mut c := m.clients[i]
|
||||
@@ -33,7 +33,7 @@ fn start_server() ? {
|
||||
}
|
||||
}, s)
|
||||
|
||||
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
||||
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')) }
|
||||
|
@@ -6,27 +6,27 @@ import net.websocket
|
||||
fn main() {
|
||||
go start_server()
|
||||
time.sleep(100 * time.millisecond)
|
||||
start_client()?
|
||||
start_client()!
|
||||
}
|
||||
|
||||
// start_server starts the websocket server, it receives messages
|
||||
// and send it back to the client that sent it
|
||||
fn start_server() ? {
|
||||
fn start_server() ! {
|
||||
mut s := websocket.new_server(.ip6, 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 {
|
||||
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
|
||||
})?
|
||||
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
||||
})!
|
||||
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
|
||||
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||
})
|
||||
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
||||
s.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
|
||||
// println('client ($ws.id) closed connection')
|
||||
})
|
||||
s.listen() or { println('error on server listen: $err') }
|
||||
@@ -37,23 +37,23 @@ fn start_server() ? {
|
||||
|
||||
// start_client starts the websocket client, it writes a message to
|
||||
// the server and prints all the messages received
|
||||
fn start_client() ? {
|
||||
mut ws := websocket.new_client('ws://localhost:30000')?
|
||||
fn start_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) ? {
|
||||
ws.on_open(fn (mut ws websocket.Client) ! {
|
||||
println('open!')
|
||||
})
|
||||
// use on_error_ref if you want to send any reference object
|
||||
ws.on_error(fn (mut ws websocket.Client, err string) ? {
|
||||
ws.on_error(fn (mut ws websocket.Client, err string) ! {
|
||||
println('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) ? {
|
||||
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
|
||||
println('closed')
|
||||
})
|
||||
// use on_message_ref if you want to send any reference object
|
||||
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
||||
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
|
||||
if msg.payload.len > 0 {
|
||||
message := msg.payload.bytestr()
|
||||
println('client got type: $msg.opcode payload:\n$message')
|
||||
@@ -72,7 +72,7 @@ fn start_client() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn write_echo(mut ws websocket.Client) ? {
|
||||
fn write_echo(mut ws websocket.Client) ! {
|
||||
message := 'echo this'
|
||||
for i := 0; i <= 10; i++ {
|
||||
// Server will send pings every 30 seconds
|
||||
|
Reference in New Issue
Block a user