-
Notifications
You must be signed in to change notification settings - Fork 2
/
realm.go
127 lines (100 loc) · 2.96 KB
/
realm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package xconn
import (
"fmt"
"github.com/xconnio/wampproto-go"
"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/xconn-go/internal"
)
type Realm struct {
broker *wampproto.Broker
dealer *wampproto.Dealer
clients internal.Map[int64, BaseSession]
}
func NewRealm() *Realm {
return &Realm{
broker: wampproto.NewBroker(),
dealer: wampproto.NewDealer(),
clients: internal.Map[int64, BaseSession]{},
}
}
func (r *Realm) AttachClient(base BaseSession) error {
r.clients.Store(base.ID(), base)
details := wampproto.NewSessionDetails(base.ID(), base.Realm(), base.AuthID(), base.AuthRole(),
base.Serializer().Static())
if err := r.broker.AddSession(details); err != nil {
return err
}
if err := r.dealer.AddSession(details); err != nil {
return err
}
return nil
}
func (r *Realm) DetachClient(base BaseSession) error {
r.clients.Delete(base.ID())
if err := r.broker.RemoveSession(base.ID()); err != nil {
return err
}
if err := r.dealer.RemoveSession(base.ID()); err != nil {
return err
}
return nil
}
func (r *Realm) ReceiveMessage(sessionID int64, msg messages.Message) error {
switch msg.Type() {
case messages.MessageTypeCall, messages.MessageTypeYield, messages.MessageTypeRegister,
messages.MessageTypeUnregister, messages.MessageTypeError:
msgWithRecipient, err := r.dealer.ReceiveMessage(sessionID, msg)
if err != nil {
return err
}
client, _ := r.clients.Load(msgWithRecipient.Recipient)
return client.WriteMessage(msgWithRecipient.Message)
case messages.MessageTypeSubscribe, messages.MessageTypeUnsubscribe:
msgWithRecipient, err := r.broker.ReceiveMessage(sessionID, msg)
if err != nil {
return err
}
client, _ := r.clients.Load(msgWithRecipient.Recipient)
return client.WriteMessage(msgWithRecipient.Message)
case messages.MessageTypePublish:
publish := msg.(*messages.Publish)
publication, err := r.broker.ReceivePublish(sessionID, publish)
if err != nil {
return err
}
for _, recipientID := range publication.Recipients {
client, _ := r.clients.Load(recipientID)
_ = client.WriteMessage(publication.Event)
}
if publication.Ack != nil {
client, _ := r.clients.Load(publication.Ack.Recipient)
_ = client.WriteMessage(publication.Ack.Message)
}
return nil
case messages.MessageTypeGoodbye:
if err := r.dealer.RemoveSession(sessionID); err != nil {
return err
}
client, exists := r.clients.LoadAndDelete(sessionID)
if !exists {
return fmt.Errorf("goodbye: client does not exist")
}
goodbye := messages.NewGoodBye(CloseGoodByeAndOut, nil)
if err := client.WriteMessage(goodbye); err != nil {
return err
}
_ = client.Close()
return nil
default:
return fmt.Errorf("unknown message type: %v", msg.Type())
}
}
func (r *Realm) Close() {
goodbye := messages.NewGoodBye(CloseSystemShutdown, nil)
r.clients.Range(func(id int64, client BaseSession) bool {
_ = client.WriteMessage(goodbye)
_ = client.Close()
_ = r.DetachClient(client)
return true
})
}