@@ -5,13 +5,12 @@ package websocket
55import (
66 "bufio"
77 "context"
8- cryptorand "crypto/rand"
8+ "crypto/rand"
99 "errors"
1010 "fmt"
1111 "io"
1212 "io/ioutil"
1313 "log"
14- "math/rand"
1514 "runtime"
1615 "strconv"
1716 "sync"
@@ -82,6 +81,7 @@ type Conn struct {
8281 setReadTimeout chan context.Context
8382 setWriteTimeout chan context.Context
8483
84+ pingCounter * atomicInt64
8585 activePingsMu sync.Mutex
8686 activePings map [string ]chan <- struct {}
8787}
@@ -100,6 +100,7 @@ func (c *Conn) init() {
100100 c .setReadTimeout = make (chan context.Context )
101101 c .setWriteTimeout = make (chan context.Context )
102102
103+ c .pingCounter = & atomicInt64 {}
103104 c .activePings = make (map [string ]chan <- struct {})
104105
105106 c .writeHeaderBuf = makeWriteHeaderBuf ()
@@ -669,7 +670,7 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte
669670 c .writeHeader .payloadLength = int64 (len (p ))
670671
671672 if c .client {
672- _ , err := io .ReadFull (cryptorand .Reader , c .writeHeader .maskKey [:])
673+ _ , err := io .ReadFull (rand .Reader , c .writeHeader .maskKey [:])
673674 if err != nil {
674675 return 0 , fmt .Errorf ("failed to generate masking key: %w" , err )
675676 }
@@ -839,10 +840,6 @@ func (c *Conn) writeClose(p []byte, cerr error) error {
839840 return nil
840841}
841842
842- func init () {
843- rand .Seed (time .Now ().UnixNano ())
844- }
845-
846843// Ping sends a ping to the peer and waits for a pong.
847844// Use this to measure latency or ensure the peer is responsive.
848845// Ping must be called concurrently with Reader as it does
@@ -851,10 +848,9 @@ func init() {
851848//
852849// TCP Keepalives should suffice for most use cases.
853850func (c * Conn ) Ping (ctx context.Context ) error {
854- id := rand .Uint64 ()
855- p := strconv .FormatUint (id , 10 )
851+ p := c .pingCounter .Increment (1 )
856852
857- err := c .ping (ctx , p )
853+ err := c .ping (ctx , strconv . FormatInt ( p , 10 ) )
858854 if err != nil {
859855 return fmt .Errorf ("failed to ping: %w" , err )
860856 }
0 commit comments