Skip to content

Commit

Permalink
websocket: add Tx / Rx method to websocket client
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkolasg committed Feb 8, 2017
1 parent 130a84f commit 7d9da75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ type Client struct {
conn *websocket.Conn
// whether to keep the connection
keep bool
rx uint64
tx uint64
sync.Mutex
}

Expand Down Expand Up @@ -228,11 +230,13 @@ func (c *Client) Send(dst *network.ServerIdentity, path string, buf []byte) ([]b
if err := c.conn.WriteMessage(websocket.BinaryMessage, buf); err != nil {
return nil, NewClientError(err)
}
c.tx += uint64(len(buf))
_, rcv, err := c.conn.ReadMessage()
if err != nil {
return nil, NewClientError(err)
}
log.Lvlf4("Received %x", rcv)
c.rx += uint64(len(buf))
return rcv, nil
}

Expand Down Expand Up @@ -291,6 +295,22 @@ func (c *Client) Close() error {
return nil
}

// Tx returns the number of bytes transmitted by this Client. It implements
// the monitor.CounterIOMeasure interface.
func (c *Client) Tx() uint64 {
c.Lock()
defer c.Unlock()
return c.tx
}

// Rx returns the number of bytes read by this Client. It implements
// the monitor.CounterIOMeasure interface.
func (c *Client) Rx() uint64 {
c.Lock()
defer c.Unlock()
return c.rx
}

// ClientError allows for returning error-codes and error-messages. It is
// implemented by cerror, that can be instantiated using NewClientError and
// NewClientErrorCode.
Expand Down
2 changes: 2 additions & 0 deletions websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func TestClient_Send(t *testing.T) {
sr := &SimpleResponse{}
log.ErrFatal(client.SendProtobuf(servers[0].ServerIdentity, r, sr))
assert.Equal(t, sr.Val, 10)
assert.NotEqual(t, uint64(0), client.Rx())
assert.NotEqual(t, uint64(0), client.Tx())
}

func TestClient_Parallel(t *testing.T) {
Expand Down

0 comments on commit 7d9da75

Please sign in to comment.