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

net.websocket: make logger configurable (#14998)

This commit is contained in:
Ken 2022-07-09 23:39:07 +09:00 committed by GitHub
parent 739f3cd90b
commit 1ae11b41e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View File

@ -36,11 +36,11 @@ pub mut:
header http.Header // headers that will be passed when connecting
conn &net.TcpConn // underlying TCP socket connection
nonce_size int = 16 // size of nounce used for masking
panic_on_callback bool // set to true of callbacks can panic
state State // current state of connection
logger &log.Log // logger used to log messages
resource_name string // name of current resource
last_pong_ut i64 // last time in unix time we got a pong message
panic_on_callback bool // set to true of callbacks can panic
state State // current state of connection
logger &log.Logger // logger used to log messages
resource_name string // name of current resource
last_pong_ut i64 // last time in unix time we got a pong message
}
// Flag represents different types of headers in websocket handshake
@ -79,6 +79,9 @@ pub enum OPCode {
pub struct ClientOpt {
read_timeout i64 = 30 * time.second
write_timeout i64 = 30 * time.second
logger &log.Logger = &log.Logger(&log.Log{
level: .info
})
}
// new_client instance a new websocket client
@ -89,9 +92,7 @@ pub fn new_client(address string, opt ClientOpt) ?&Client {
is_server: false
ssl_conn: openssl.new_ssl_conn()
is_ssl: address.starts_with('wss')
logger: &log.Log{
level: .info
}
logger: opt.logger
uri: uri
state: .closed
id: rand.uuid_v4()

View File

@ -9,7 +9,7 @@ import rand
// Server represents a websocket server connection
pub struct Server {
mut:
logger &log.Log // logger used to log
logger &log.Logger // logger used to log
ls &net.TcpListener // listener used to get incoming connection to socket
accept_client_callbacks []AcceptClientFn // accept client callback functions
message_callbacks []MessageEventHandler // new message callback functions
@ -34,15 +34,20 @@ pub mut:
client &Client
}
[params]
pub struct ServerOpt {
logger &log.Logger = &log.Logger(&log.Log{
level: .info
})
}
// new_server instance a new websocket server on provided port and route
pub fn new_server(family net.AddrFamily, port int, route string) &Server {
pub fn new_server(family net.AddrFamily, port int, route string, opt ServerOpt) &Server {
return &Server{
ls: 0
family: family
port: port
logger: &log.Log{
level: .info
}
logger: opt.logger
state: .closed
}
}