Handeling websocket disconnect

This commit is contained in:
2023-09-19 10:19:04 -05:00
parent 8f0db70487
commit 832e02f42f
6 changed files with 46 additions and 14 deletions

View File

@ -22,6 +22,8 @@ type Dispatcher struct {
extensions message.Extensions
advice atomic.Value
//map requestID
pendingSubs map[string]chan error //todo wrap in structure
pendingSubsMu sync.Mutex
@ -103,6 +105,19 @@ func (d *Dispatcher) dispatchMessage(msg *message.Message) {
if message.IsMetaMessage(msg) {
//handle it
switch msg.Channel {
case message.MetaConnect:
if msg.Advice.Reconnect == message.ReconnectNone {
d.Disconnect()
log.Println("Websocket terminated")
return
}
log.Println("Websocket terminated: reconnecting")
d.Connect()
subsList := d.store.GetAll()
for i := range subsList {
sub := subsList[i]
d.Subscribe(sub.Name())
}
case message.MetaSubscribe:
//handle MetaSubscribe resp
d.pendingSubsMu.Lock()

View File

@ -84,6 +84,19 @@ func (s *SubscriptionsStore) RemoveAll() {
s.mutex.Unlock()
}
func (s *SubscriptionsStore) GetAll() []*subscription.Subscription {
s.mutex.Lock()
subsList := make([]*subscription.Subscription, 0)
for i := range s.subs {
//close all listeners
for j := range s.subs[i] {
subsList = append(subsList, s.subs[i][j])
}
}
s.mutex.Unlock()
return subsList
}
// Count return the number of subscriptions associated with the specified channel
func (s *SubscriptionsStore) Count(channel string) int {
return len(s.Match(channel))

View File

@ -1,7 +1,7 @@
package store
import (
"github.com/thesyncim/faye/subscription"
"gitea.watsonlabs.net/watsonb8/fayec/subscription"
"reflect"
"testing"
)