diff --git a/README.md b/README.md index 36d7c2a..c5a5b3e 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ # recws +***This is fork of [recws-org/recws](https://github.com/recws-org/recws) with some fixes*** + Reconnecting WebSocket is a websocket client based on [gorilla/websocket](https://github.com/gorilla/websocket) that will automatically reconnect if the connection is dropped - thread safe! [![Build Status](https://travis-ci.com/recws-org/recws.svg?branch=master)](https://travis-ci.com/recws-org/recws) @@ -12,7 +14,7 @@ Reconnecting WebSocket is a websocket client based on [gorilla/websocket](https: ## Installation ```bash -go get github.com/recws-org/recws +go get github.com/nikepan/recws ``` ## Sponsors @@ -21,8 +23,8 @@ go get github.com/recws-org/recws ## Logo -- Logo by [Anastasia Marx](https://www.behance.net/AnastasiaMarx) -- Gopher by [Gophers](https://github.com/egonelbre/gophers) +- Logo by [Anastasia Marx](https://www.behance.net/AnastasiaMarx) +- Gopher by [Gophers](https://github.com/egonelbre/gophers) ## License diff --git a/examples/basic.go b/examples/basic.go index a234ad0..4a8df37 100644 --- a/examples/basic.go +++ b/examples/basic.go @@ -2,9 +2,10 @@ package main import ( "context" - "github.com/recws-org/recws" "log" "time" + + "github.com/nikepan/recws" ) func main() { diff --git a/go.mod b/go.mod index 5f8b11d..047a5bd 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module github.com/recws-org/recws +module github.com/nikepan/recws -go 1.17 +go 1.18 require ( github.com/gorilla/websocket v1.4.2 diff --git a/recws.go b/recws.go index d682e0c..72af942 100644 --- a/recws.go +++ b/recws.go @@ -97,13 +97,12 @@ func (rc *RecConn) getConn() *websocket.Conn { // Close closes the underlying network connection without // sending or waiting for a close frame. func (rc *RecConn) Close() { - if rc.getConn() != nil { - rc.mu.Lock() + rc.mu.Lock() + if rc.Conn != nil { rc.Conn.Close() - rc.mu.Unlock() } - - rc.setIsConnected(false) + rc.isConnected = false + rc.mu.Unlock() } // Shutdown gracefully closes the connection by sending the websocket.CloseMessage. @@ -434,7 +433,8 @@ func (rc *RecConn) keepAlive() { } <-ticker.C - if time.Since(keepAliveResponse.getLastResponse()) > rc.getKeepAliveTimeout() { + timeoutOffset := time.Millisecond * 500 + if time.Since(keepAliveResponse.getLastResponse()) > rc.getKeepAliveTimeout()+timeoutOffset { rc.log(LogValues{Err: errors.New("keepalive timeout"), Msg: "Reconnect", Url: rc.url}) rc.CloseAndReconnect() return