diff --git a/vlib/net/websocket/utils.v b/vlib/net/websocket/utils.v index c1bfec04de..213ac77fc5 100644 --- a/vlib/net/websocket/utils.v +++ b/vlib/net/websocket/utils.v @@ -42,11 +42,11 @@ fn create_key_challenge_response(seckey string) string { return b64 } -fn get_nonce() string { - mut nonce := []byte{} +fn get_nonce(nonce_size int) string { + mut nonce := []byte{len: nonce_size, cap: nonce_size} alphanum := '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz' - for _ in 0 .. 18 { - nonce << alphanum[rand.next(61)] + for i in 0 .. nonce_size { + nonce[i] = alphanum[rand.next(61)] } - return string(byteptr(nonce.data)) + return tos(nonce.data, nonce.len).clone() } diff --git a/vlib/net/websocket/ws.v b/vlib/net/websocket/ws.v index 489f873647..1398e3c047 100644 --- a/vlib/net/websocket/ws.v +++ b/vlib/net/websocket/ws.v @@ -19,6 +19,7 @@ pub struct Client { // cwebsocket_subprotocol *subprotocol; // cwebsocket_subprotocol *subprotocols[]; mut: + nonce_size int = 18 // you can try 16 too lock &sync.Mutex = sync.new_mutex() write_lock &sync.Mutex = sync.new_mutex() state State @@ -137,7 +138,7 @@ pub fn (mut ws Client) connect() int { ws.state = .connecting ws.lock.unlock() uri := ws.parse_uri() - nonce := get_nonce() + nonce := get_nonce(ws.nonce_size) seckey := base64.encode(nonce) ai_family := C.AF_INET ai_socktype := C.SOCK_STREAM