Skip to content
Open
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
11 changes: 11 additions & 0 deletions wetsock/wetsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"encoding/json"
"errors"
"reflect"
"sync"

"github.com/gorilla/websocket"
"github.com/tv42/birpc"
)

type codec struct {
WS *websocket.Conn
// https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
// As above document.Only one concurrent reader and one concurrent writer are allowed.
readMu sync.Mutex
writeMu sync.Mutex
}

// This is ugly, but i need to override the unmarshaling logic for
Expand All @@ -27,6 +32,9 @@ type jsonMessage struct {
}

func (c *codec) ReadMessage(msg *birpc.Message) error {
c.readMu.Lock()
defer c.readMu.Unlock()

var jm jsonMessage
err := c.WS.ReadJSON(&jm)
if err != nil {
Expand All @@ -41,6 +49,9 @@ func (c *codec) ReadMessage(msg *birpc.Message) error {
}

func (c *codec) WriteMessage(msg *birpc.Message) error {
c.writeMu.Lock()
defer c.writeMu.Unlock()

return c.WS.WriteJSON(msg)
}

Expand Down