1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.websocket: allow timeout to be configured (#14941)

This commit is contained in:
Ken
2022-07-05 12:40:23 +09:00
committed by GitHub
parent 5523c11dd4
commit a50e0f0522
2 changed files with 20 additions and 28 deletions

View File

@@ -27,9 +27,11 @@ mut:
open_callbacks []OpenEventHandler // all callbacks on_open
close_callbacks []CloseEventHandler // all callbacks on_close
pub:
is_ssl bool // true if secure socket is used
uri Uri // uri of current connection
id string // unique id of client
is_ssl bool // true if secure socket is used
uri Uri // uri of current connection
id string // unique id of client
read_timeout i64
write_timeout i64
pub mut:
header http.Header // headers that will be passed when connecting
conn &net.TcpConn // underlying TCP socket connection
@@ -73,8 +75,14 @@ pub enum OPCode {
pong = 0x0A
}
[params]
pub struct ClientOpt {
read_timeout i64 = 30 * time.second
write_timeout i64 = 30 * time.second
}
// new_client instance a new websocket client
pub fn new_client(address string) ?&Client {
pub fn new_client(address string, opt ClientOpt) ?&Client {
uri := parse_uri(address)?
return &Client{
conn: 0
@@ -88,6 +96,8 @@ pub fn new_client(address string) ?&Client {
state: .closed
id: rand.uuid_v4()
header: http.new_header()
read_timeout: opt.read_timeout
write_timeout: opt.write_timeout
}
}
@@ -97,9 +107,6 @@ pub fn (mut ws Client) connect() ? {
ws.set_state(.connecting)
ws.logger.info('connecting to host $ws.uri')
ws.conn = ws.dial_socket()?
// Todo: make setting configurable
ws.conn.set_read_timeout(time.second * 30)
ws.conn.set_write_timeout(time.second * 30)
ws.handshake()?
ws.set_state(.open)
ws.logger.info('successfully connected to host $ws.uri')