From e771a68cebe18b0913373a4fa24097ffa9992613 Mon Sep 17 00:00:00 2001 From: Omer Akram Date: Thu, 4 Jul 2024 03:25:19 +0500 Subject: [PATCH] fix goodbye and unsubscribe --- messages/goodbye.go | 4 ++++ session.go | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/messages/goodbye.go b/messages/goodbye.go index 2f26a60..d49c8df 100644 --- a/messages/goodbye.go +++ b/messages/goodbye.go @@ -40,6 +40,10 @@ type GoodBye struct { func NewGoodByeWithFields(fields GoodByeFields) *GoodBye { return &GoodBye{GoodByeFields: fields} } func NewGoodBye(reason string, details map[string]any) *GoodBye { + if details == nil { + details = map[string]any{} + } + return &GoodBye{GoodByeFields: &goodByeFields{reason: reason, details: details}} } diff --git a/session.go b/session.go index 4552892..029f1d9 100644 --- a/session.go +++ b/session.go @@ -94,7 +94,7 @@ func (w *Session) SendMessage(msg messages.Message) ([]byte, error) { unsubscribe.SubscriptionID()) } - w.unsubscribeRequests.Store(unsubscribe.RequestID(), unsubscribe.RequestID()) + w.unsubscribeRequests.Store(unsubscribe.RequestID(), unsubscribe.SubscriptionID()) return data, nil case messages.MessageTypeError: @@ -105,6 +105,8 @@ func (w *Session) SendMessage(msg messages.Message) ([]byte, error) { w.invocationRequests.Delete(errorMsg.RequestID()) return data, nil + case messages.MessageTypeGoodbye: + return data, nil default: return nil, fmt.Errorf("send not supported for message of type %T", msg) } @@ -188,7 +190,7 @@ func (w *Session) ReceiveMessage(msg messages.Message) (messages.Message, error) _, exists = w.subscriptions.LoadAndDelete(subscriptionID) if !exists { - return nil, fmt.Errorf("received UNSUBSCRIBED for invalid subscriptionID") + return nil, fmt.Errorf("received UNSUBSCRIBED for invalid subscriptionID %d", subscriptionID) } return unsubscribed, nil @@ -248,6 +250,8 @@ func (w *Session) ReceiveMessage(msg messages.Message) (messages.Message, error) default: return nil, fmt.Errorf("unknown error message type %T", msg) } + case messages.MessageTypeGoodbye: + return msg, nil default: return nil, fmt.Errorf("unknown message type %T", msg) }