diff --git a/websocket.go b/websocket.go index b34d908..cc363af 100644 --- a/websocket.go +++ b/websocket.go @@ -2,6 +2,7 @@ package turnpike import ( "fmt" + "sync" "time" "github.com/gorilla/websocket" @@ -13,6 +14,7 @@ type websocketPeer struct { messages chan Message payloadType int closed bool + writeMutex sync.Mutex } // NewWebsocketPeer connects to the websocket server at the specified url. @@ -56,6 +58,8 @@ func (ep *websocketPeer) Send(msg Message) error { if err != nil { return err } + ep.writeMutex.Lock() + defer ep.writeMutex.Unlock() return ep.conn.WriteMessage(ep.payloadType, b) } func (ep *websocketPeer) Receive() <-chan Message {