Serialise websocket writes as per https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
This commit is contained in:
parent
742329d48f
commit
2b866fdcb4
16
client.go
16
client.go
@ -9,6 +9,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@ -26,12 +27,13 @@ func Log() *log.Logger {
|
|||||||
type EventHandler func(ctx context.Context, client *Client, event Event) error
|
type EventHandler func(ctx context.Context, client *Client, event Event) error
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
params RegistrationParams
|
params RegistrationParams
|
||||||
c *websocket.Conn
|
c *websocket.Conn
|
||||||
actions map[string]*Action
|
actions map[string]*Action
|
||||||
handlers map[string][]EventHandler
|
handlers map[string][]EventHandler
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
sendMutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ctx context.Context, params RegistrationParams) *Client {
|
func NewClient(ctx context.Context, params RegistrationParams) *Client {
|
||||||
@ -140,6 +142,8 @@ func (client *Client) register(params RegistrationParams) error {
|
|||||||
|
|
||||||
func (client *Client) send(event Event) error {
|
func (client *Client) send(event Event) error {
|
||||||
j, _ := json.Marshal(event)
|
j, _ := json.Marshal(event)
|
||||||
|
client.sendMutex.Lock()
|
||||||
|
defer client.sendMutex.Unlock()
|
||||||
logger.Printf("sending message: %v\n", string(j))
|
logger.Printf("sending message: %v\n", string(j))
|
||||||
return client.c.WriteJSON(event)
|
return client.c.WriteJSON(event)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user