Skip to content

Commit

Permalink
minor: add Broadcast and BroadcastOthers helpers on NSConn and update…
Browse files Browse the repository at this point in the history
… dependencies
  • Loading branch information
kataras committed Nov 14, 2024
1 parent 23b9b4b commit 07c1e09
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func (c *Conn) notifyNamespaceConnected(ns *NSConn, connectMsg Message) {
}
}

func (c *Conn) notifyNamespaceDisconnect(ns *NSConn, disconnectMsg Message) {
func (c *Conn) notifyNamespaceDisconnect(_ *NSConn, disconnectMsg Message) {
if !c.IsClient() && c.server.usesStackExchange() {
c.server.StackExchange.Unsubscribe(c, disconnectMsg.Namespace)
}
Expand Down
10 changes: 10 additions & 0 deletions conn_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func (ns *NSConn) String() string {
return ns.Conn.String()
}

// Broadcast method sends a message to all connections, including this one.
func (ns *NSConn) Broadcast(msgs ...Message) {
ns.Conn.server.Broadcast(nil, msgs...)
}

// Broadcast method sends a message to all connections except this one.
func (ns *NSConn) BroadcastOthers(msgs ...Message) {
ns.Conn.server.Broadcast(ns.Conn, msgs...)
}

// Emit method sends a message to the remote side
// with its `Message.Namespace` filled to this specific namespace.
func (ns *NSConn) Emit(event string, body []byte) bool {
Expand Down
4 changes: 2 additions & 2 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package neffos_test
import (
"bytes"
"context"
"fmt"
"errors"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestOnNativeMessageAndMessageError(t *testing.T) {
serverHandler := neffos.JoinConnHandlers(neffos.Namespaces{namespace: events},
neffos.Events{
eventThatWillGiveErrorByServer: func(c *neffos.NSConn, msg neffos.Message) error {
return fmt.Errorf(eventErrorText)
return errors.New(eventErrorText)
},
})
teardownServer := runTestServer("localhost:8080", serverHandler)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ require (
require (
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
)
12 changes: 6 additions & 6 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func serializeOutput(wait, namespace, room, event string,
// and returns a neffos Message.
// When allowNativeMessages only Body is filled and check about message format is skipped.
func DeserializeMessage(msgTyp MessageType, b []byte, allowNativeMessages, shouldHandleOnlyNativeMessages bool) Message {
wait, namespace, room, event, body, err, isNoOp, isInvalid := deserializeInput(b, allowNativeMessages, shouldHandleOnlyNativeMessages)
wait, namespace, room, event, body, isNoOp, isInvalid, err := deserializeInput(b, allowNativeMessages, shouldHandleOnlyNativeMessages)

fromExplicit := ""
if isServerConnID(wait) {
Expand Down Expand Up @@ -421,9 +421,9 @@ func deserializeInput(b []byte, allowNativeMessages, shouldHandleOnlyNativeMessa
room,
event string,
body []byte,
err error,
isNoOp bool,
isInvalid bool,
err error,
) {

if len(b) == 0 {
Expand Down

0 comments on commit 07c1e09

Please sign in to comment.