Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #25

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 38 additions & 50 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ func NewClient(address string) (Client, error) {
c.connected = false
c.handshakeTimeout = 3 * time.Second
c.dialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: c.handshakeTimeout,
ReadBufferSize: 512 * 1024,
WriteBufferSize: 512 * 1024,
Jar: nil,
}

_ = c.Start()

//
go func() {
_, _ = c.connect(false)
}()
Expand All @@ -142,8 +143,6 @@ func (c *client) setStarted(b bool) {
}

func (c *client) getStarted() bool {
c.mut.RLock()
defer c.mut.RUnlock()
return c.started
}

Expand All @@ -154,9 +153,6 @@ func (c *client) setConnected(b bool) {
}

func (c *client) isConnected() bool {
c.mut.RLock()
defer c.mut.RUnlock()

return c.connected
}

Expand All @@ -167,23 +163,14 @@ func (c *client) setSubscribed(b bool) {
}

func (c *client) getSubscribed() bool {
c.mut.RLock()
defer c.mut.RUnlock()

return c.subscribed
}

func (c *client) getConn() *websocket.Conn {
c.mut.RLock()
defer c.mut.RUnlock()

return c.conn
}

func (c *client) getSubscribedAddress() map[string]bool {
c.mut.RLock()
defer c.mut.RUnlock()

return c.subscribedAddresses
}

Expand Down Expand Up @@ -326,40 +313,40 @@ func (c *client) listenWrite() {
}

func (c *client) listen() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-c.listenCtx.Done():
return
case <-ticker.C:
for {
if !c.isConnected() {
continue
}
listening := true
for listening {
go func() {
select {
case <-c.listenCtx.Done():
listening = false
break
}
}()
if !c.isConnected() {
time.Sleep(time.Second * 1)
continue
}

messageType, readingMessage, err := c.readMessage()
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
_ = c.Stop()
return
}
if err != nil {
c.closeWS()
continue
}
messageType, readingMessage, err := c.readMessage()
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
_ = c.Stop()
return
}
if err != nil {
c.closeWS()
continue
}

switch messageType {
case websocket.TextMessage:
if json.Valid(readingMessage) {
var transaction Transaction
if err := json.Unmarshal(readingMessage, &transaction); err == nil {
c.listenCallback(transaction)
}
} else {
//
//
}
switch messageType {
case websocket.TextMessage:
if json.Valid(readingMessage) {
var transaction Transaction
if err := json.Unmarshal(readingMessage, &transaction); err == nil {
c.listenCallback(transaction)
}
} else {
//
//
}
}
}
Expand Down Expand Up @@ -394,22 +381,23 @@ func (c *client) ping() {

}
case <-c.mainCtx.Done():
c.pingTicker.Stop()
return
}
}
}

func (c *client) closeWS() {
if c.getConn() != nil {
if conn := c.getConn(); conn != nil {
_ = c.write(sendMsg{
typ: mclose,
messageTyp: websocket.CloseMessage,
msg: websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
})
_ = c.conn.Close()
c.connected = false
c.conn = nil
_ = conn.Close()
}
c.connected = false
c.conn = nil
}

// SetListenCallback Callback that will be called when the WS client captures a
Expand Down