diff --git a/makefile b/makefile new file mode 100644 index 0000000..c86c9ee --- /dev/null +++ b/makefile @@ -0,0 +1,8 @@ +install: + go get -t -v -u ./... + +linter: + golangci-lint run --enable-all + +test: + make linter \ No newline at end of file diff --git a/recws.go b/recws.go index 5fbfb5f..62ba6c4 100644 --- a/recws.go +++ b/recws.go @@ -36,20 +36,20 @@ type RecConn struct { // Proxy specifies the proxy function for the dialer // defaults to ProxyFromEnvironment Proxy func(*http.Request) (*url.URL, error) - // NonVerbose suppress connecting/reconnecting messages. - NonVerbose bool // SubscribeHandler fires after the connection successfully establish. SubscribeHandler func() error // KeepAliveTimeout is an interval for sending ping/pong messages // disabled if 0 KeepAliveTimeout time.Duration + // NonVerbose suppress connecting/reconnecting messages. + NonVerbose bool + isConnected bool mu sync.RWMutex url string reqHeader http.Header httpResp *http.Response dialErr error - isConnected bool dialer *websocket.Dialer *websocket.Conn @@ -249,7 +249,7 @@ func (rc *RecConn) setDefaultDialer(handshakeTimeout time.Duration) { rc.dialer = &websocket.Dialer{ HandshakeTimeout: handshakeTimeout, - Proxy: rc.Proxy, + Proxy: rc.Proxy, } } @@ -354,13 +354,16 @@ func (rc *RecConn) keepAlive() { defer ticker.Stop() for { - if (!rc.IsConnected()) { - continue; + if !rc.IsConnected() { + continue + } + + if err := rc.writeControlPingMessage(); err != nil { + log.Println(err) } - rc.writeControlPingMessage() <-ticker.C - if time.Now().Sub(keepAliveResponse.getLastResponse()) > rc.getKeepAliveTimeout() { + if time.Since(keepAliveResponse.getLastResponse()) > rc.getKeepAliveTimeout() { rc.closeAndReconnect() return }