diff --git a/conn.go b/conn.go index fc85e0c..f1737e3 100644 --- a/conn.go +++ b/conn.go @@ -423,6 +423,7 @@ func (c *Conn) handleQueue() { // In the future it may be exposed by an error listener. var ErrInvalidPayload = errors.New("invalid payload") +// handle message from client via socket func (c *Conn) handleMessage(msg Message) error { if msg.isInvalid { return ErrInvalidPayload diff --git a/conn_namespace.go b/conn_namespace.go index 81d79d2..d00670f 100644 --- a/conn_namespace.go +++ b/conn_namespace.go @@ -34,6 +34,7 @@ type NSConn struct { value reflect.Value Party Party + Guild Guild friends *Friends diff --git a/conn_party.go b/conn_party.go index 764ba2d..d27fc2c 100644 --- a/conn_party.go +++ b/conn_party.go @@ -2,6 +2,7 @@ package wolfsocket import ( "errors" + "github.com/WolffunService/wolfsocket/wserror" ) @@ -22,6 +23,8 @@ func (ns *NSConn) forceLeaveAll() { Event: OnPartyLeave, }) + ns.DisconnectGuild() + ns.leaveAllRoomChat() } @@ -247,3 +250,27 @@ func (ns *NSConn) replyJoined() { ns.Conn.Write(msg) } + +func (ns *NSConn) DisconnectGuild() error { + if ns == nil { + return nil + } + + // guild := ns.Guild + // if guild == nil { + // return nil + // } + + msg := Message{ + Namespace: ns.namespace, + Event: OnGuildDisconnect, + IsLocal: true, + } + + err := ns.events.fireEvent(ns, msg) + if err != nil { + return err + } + + return nil +} diff --git a/event.go b/event.go index fb8391a..730da48 100644 --- a/event.go +++ b/event.go @@ -66,6 +66,14 @@ var ( OnChat = "_OnChat" OnReceiveMsgChat = "_ReceiveMsgChat" + + // guild: connect, join, disconnect, leave,... + // "application": + //- connect: incr + ws.connect + //- join: check in party + call connect + // disconnect, leave, kick: similar + OnGuildConnect = "_OnGuildConnect" // check in guild then incr + ws.connect + OnGuildDisconnect = "_OnGuildDisconnect" // if not nil then decr + ws.disconnect ) // IsSystemEvent reports whether the "event" is a system event, @@ -77,7 +85,8 @@ func IsSystemEvent(event string) bool { case OnNamespaceConnect, OnNamespaceConnected, OnNamespaceDisconnect, OnRoomJoin, OnRoomJoined, OnRoomLeave, OnRoomLeft, OnPartyJoin, OnPartyJoined, OnPartyLeave, OnPartyLeft, - OnPartySomebodyJoined, OnPartySomebodyLeft: + OnPartySomebodyJoined, OnPartySomebodyLeft, + OnGuildConnect, OnGuildDisconnect: return true default: return false diff --git a/guild.go b/guild.go new file mode 100644 index 0000000..0a5c3cd --- /dev/null +++ b/guild.go @@ -0,0 +1,70 @@ +package wolfsocket + +import ( + "github.com/WolffunService/wolfsocket/options" + "github.com/WolffunService/wolfsocket/stackexchange/protos" +) + +type Guild interface { + NSConn() *NSConn + GetID() string + + Broadcast(eventName string, body []byte, opts ...options.BroadcastOption) + + Subscribe() + Unsubscribe() + + Connect() + Disconnect() +} + +var _ Guild = &BaseGuild{} + +const prefixGuild = "guild." + +type BaseGuild struct { + ID string + + nsConn *NSConn +} + +func NewGuild(guildID string) *BaseGuild { + return &BaseGuild{ + ID: guildID, + } +} + +func (p *BaseGuild) NSConn() *NSConn { + return p.nsConn +} + +func (p *BaseGuild) GetID() string { + return p.ID +} + +func (p *BaseGuild) Broadcast(eventName string, body []byte, opts ...options.BroadcastOption) { + msg := protos.ServerMessage{ + EventName: eventName, + Body: body, + } + p.nsConn.SBroadcast(p.getChannel(), msg, opts...) +} + +func (p *BaseGuild) Subscribe() { + p.nsConn.Subscribe(p.getChannel()) +} +func (p *BaseGuild) Unsubscribe() { + p.nsConn.Unsubscribe(p.getChannel()) +} + +func (p *BaseGuild) Connect() { + p.Subscribe() +} + +func (p *BaseGuild) Disconnect() { + p.Unsubscribe() +} + +func (p *BaseGuild) getChannel() string { + return prefixGuild + p.ID +} diff --git a/stackexchange/nats/stackexchange_nats.go b/stackexchange/nats/stackexchange_nats.go index a7964bc..fe60636 100644 --- a/stackexchange/nats/stackexchange_nats.go +++ b/stackexchange/nats/stackexchange_nats.go @@ -297,6 +297,7 @@ func (exc *StackExchange) OnConnect(c *wolfsocket.Conn) error { return nil } +// handle message from pubsub func (exc *StackExchange) handleMessage(natsMsg *nats.Msg, conn *wolfsocket.Conn) (err error) { if natsMsg == nil { //log