Skip to content

Commit

Permalink
Add SendRaw and BroadcastMsgRaw to websocket hub
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Apr 19, 2024
1 parent 0354332 commit 7e6e884
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
23 changes: 20 additions & 3 deletions web/websockethub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type WebsocketMsg struct {
Data []byte
}

// rawMsg is used to skip JSON marshalling while sending messages.

Check failure on line 36 in web/websockethub/client.go

View workflow job for this annotation

GitHub Actions / golangci-web

[golangci-web] web/websockethub/client.go#L36

`marshalling` is a misspelling of `marshaling` (misspell)
Raw output
websockethub/client.go:36:32: `marshalling` is a misspelling of `marshaling` (misspell)
// rawMsg is used to skip JSON marshalling while sending messages.
                               ^
type rawMsg struct {
Data []byte
}

// ClientID is the ID of a client.
type ClientID uint32

Expand Down Expand Up @@ -293,6 +298,11 @@ func (c *Client) writePump() {
ctx, cancel := context.WithTimeout(c.ctx, writeWait)
defer cancel()

// check if the message is a raw message that does not need to be JSON marshaled.
if rawMsg, ok := msg.(*rawMsg); ok {
return c.conn.Write(ctx, websocket.MessageText, rawMsg.Data)
}

return wsjson.Write(ctx, c.conn, msg)
}

Expand Down Expand Up @@ -340,7 +350,8 @@ func (c *Client) writePump() {
}

// Send sends a message to the client.
func (c *Client) Send(ctx context.Context, msg interface{}, dontDrop ...bool) error {
// JSON marshaling is done automatically based on "json" tags.
func (c *Client) Send(ctx context.Context, data interface{}, dontDrop ...bool) error {
if c.hub.Stopped() {
// hub was already shut down
return ErrWebsocketServerUnavailable
Expand Down Expand Up @@ -374,7 +385,7 @@ func (c *Client) Send(ctx context.Context, msg interface{}, dontDrop ...bool) er
return ErrClientDisconnected
case <-c.sendChanClosed:
return ErrClientDisconnected
case c.sendChan <- msg:
case c.sendChan <- data:
return nil
}
}
Expand All @@ -394,10 +405,16 @@ func (c *Client) Send(ctx context.Context, msg interface{}, dontDrop ...bool) er
return ErrClientDisconnected
default:
select {
case c.sendChan <- msg:
case c.sendChan <- data:
return nil
default:
return nil
}
}
}

// SendRaw sends a raw message to the client.
// The message is not JSON marshaled.
func (c *Client) SendRaw(ctx context.Context, data []byte, dontDrop ...bool) error {
return c.Send(ctx, &rawMsg{Data: data}, dontDrop...)
}
7 changes: 7 additions & 0 deletions web/websockethub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (h *Hub) Events() *Events {
}

// BroadcastMsg sends a message to all clients.
// JSON marshaling is done automatically based on "json" tags.
func (h *Hub) BroadcastMsg(ctx context.Context, data interface{}, dontDrop ...bool) error {
if h.shutdownFlag.Load() {
// hub was already shut down or was not started yet
Expand Down Expand Up @@ -157,6 +158,12 @@ func (h *Hub) BroadcastMsg(ctx context.Context, data interface{}, dontDrop ...bo
}
}

// BroadcastMsgRaw sends a raw message to all clients.
// The message is not JSON marshaled.
func (h *Hub) BroadcastMsgRaw(ctx context.Context, data []byte, dontDrop ...bool) error {
return h.BroadcastMsg(ctx, &rawMsg{Data: data}, dontDrop...)
}

func (h *Hub) removeClient(client *Client) {
delete(h.clients, client)
close(client.ExitSignal)
Expand Down

0 comments on commit 7e6e884

Please sign in to comment.