Skip to content

Commit

Permalink
fix: fix broadcasting from a parent namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
zishang520 committed Jul 16, 2024
1 parent bfca280 commit 22c688d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions socket/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type (
)

func (*AdapterBuilder) New(nsp Namespace) Adapter {
return NewAdapterNew(nsp)
return NewAdapter(nsp)
}

func MakeAdapter() Adapter {
Expand All @@ -45,7 +45,7 @@ func MakeAdapter() Adapter {
return a
}

func NewAdapterNew(nsp Namespace) Adapter {
func NewAdapter(nsp Namespace) Adapter {
n := MakeAdapter()

n.Construct(nsp)
Expand Down
1 change: 1 addition & 0 deletions socket/namespace-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Namespace interface {
type ParentNamespace interface {
Namespace

Children() *types.Set[Namespace]
CreateChild(string) Namespace
}

Expand Down
21 changes: 5 additions & 16 deletions socket/parent-broadcast-adapter.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
package socket

import (
"github.com/zishang520/engine.io/v2/types"
"github.com/zishang520/socket.io-go-parser/v2/parser"
)

type (
ParentBroadcastAdapterBuilder struct {
AdapterConstructor

Children *types.Set[Namespace]
}

// A dummy adapter that only supports broadcasting to child (concrete) namespaces.
parentBroadcastAdapter struct {
Adapter

children *types.Set[Namespace]
}
)

func (b *ParentBroadcastAdapterBuilder) New(nsp Namespace) Adapter {
return NewParentBroadcastAdapter(nsp, b.Children)
return NewParentBroadcastAdapter(nsp)
}

func MakeParentBroadcastAdapter(children *types.Set[Namespace]) ParentBroadcastAdapter {
func MakeParentBroadcastAdapter() ParentBroadcastAdapter {
s := &parentBroadcastAdapter{
Adapter: MakeAdapter(),

children: children,
}

s.Prototype(s)

return s
}

func NewParentBroadcastAdapter(nsp Namespace, children *types.Set[Namespace]) ParentBroadcastAdapter {
s := MakeParentBroadcastAdapter(children)
func NewParentBroadcastAdapter(nsp Namespace) ParentBroadcastAdapter {
s := MakeParentBroadcastAdapter()

s.Construct(nsp)

return s
}

func (s *parentBroadcastAdapter) Construct(nsp Namespace) {
s.Adapter.Construct(nsp)
}

func (s *parentBroadcastAdapter) Broadcast(packet *parser.Packet, opts *BroadcastOptions) {
for _, nsp := range s.children.Keys() {
for _, nsp := range s.Nsp().(ParentNamespace).Children().Keys() {
nsp.Adapter().Broadcast(packet, opts)
}
}
6 changes: 5 additions & 1 deletion socket/parent-namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *parentNamespace) Adapter() Adapter {
}

func (p *parentNamespace) InitAdapter() {
p.adapter = NewParentBroadcastAdapter(p, p.children)
p.adapter = NewParentBroadcastAdapter(p)
}

func (p *parentNamespace) Emit(ev string, args ...any) error {
Expand All @@ -70,6 +70,10 @@ func (p *parentNamespace) Emit(ev string, args ...any) error {
return nil
}

func (p *parentNamespace) Children() *types.Set[Namespace] {
return p.children
}

func (p *parentNamespace) CreateChild(name string) Namespace {
parent_namespace_log.Debug("creating child namespace %s", name)
namespace := NewNamespace(p.Server(), name)
Expand Down

0 comments on commit 22c688d

Please sign in to comment.