Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions conn_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type NSConn struct {
value reflect.Value

Party Party
Guild Guild

friends *Friends

Expand Down
27 changes: 27 additions & 0 deletions conn_party.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wolfsocket

import (
"errors"

"github.com/WolffunService/wolfsocket/wserror"
)

Expand All @@ -22,6 +23,8 @@ func (ns *NSConn) forceLeaveAll() {
Event: OnPartyLeave,
})

ns.DisconnectGuild()

ns.leaveAllRoomChat()
}

Expand Down Expand Up @@ -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
}
11 changes: 10 additions & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
70 changes: 70 additions & 0 deletions guild.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions stackexchange/nats/stackexchange_nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down