Skip to content

Commit 3ffd997

Browse files
authored
Merge pull request #1859 from dedis/work-be1-stuart-fix-connexion-problem
Fix connexion problem
2 parents 2db8b47 + e759095 commit 3ffd997

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

be1-go/internal/popserver/hub.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (h *Hub) Start() {
5656
go func() {
5757
utils.LogInfo("start the Hub")
5858
for {
59+
utils.LogInfo("waiting for a new message")
5960
select {
6061
case incomingMessage := <-h.messageChan:
6162
utils.LogInfo("start handling a message")
@@ -65,9 +66,9 @@ func (h *Hub) Start() {
6566
} else {
6667
utils.LogInfo("successfully handled a message")
6768
}
68-
case <-h.closedSockets:
69-
utils.LogInfo("stopping the Sockets")
70-
return
69+
case socketID := <-h.closedSockets:
70+
utils.LogInfo("stopping the Socket " + socketID)
71+
state.UnsubscribeFromAll(socketID)
7172
case <-h.stop:
7273
utils.LogInfo("stopping the Hub")
7374
return

be1-go/internal/popserver/state/state.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Subscriber interface {
2323
HasChannel(channel string) bool
2424
Subscribe(channel string, socket socket.Socket) *answer.Error
2525
Unsubscribe(channel string, socket socket.Socket) *answer.Error
26+
UnsubscribeFromAll(socketID string)
2627
SendToAll(buf []byte, channel string) *answer.Error
2728
}
2829

@@ -104,6 +105,17 @@ func Unsubscribe(socket socket.Socket, channel string) *answer.Error {
104105
return subs.Unsubscribe(channel, socket)
105106
}
106107

108+
func UnsubscribeFromAll(socketID string) *answer.Error {
109+
subs, errAnswer := getSubs()
110+
if errAnswer != nil {
111+
return errAnswer
112+
}
113+
114+
subs.UnsubscribeFromAll(socketID)
115+
116+
return nil
117+
}
118+
107119
func SendToAll(buf []byte, channel string) *answer.Error {
108120
subs, errAnswer := getSubs()
109121
if errAnswer != nil {

be1-go/internal/popserver/types/subscribers.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"fmt"
45
"golang.org/x/xerrors"
56
"popstellar/message/answer"
67
"popstellar/network/socket"
@@ -65,6 +66,20 @@ func (s *Subscribers) Unsubscribe(channel string, socket socket.Socket) *answer.
6566
return nil
6667
}
6768

69+
func (s *Subscribers) UnsubscribeFromAll(socketID string) {
70+
s.Lock()
71+
defer s.Unlock()
72+
73+
for channel, subs := range s.list {
74+
_, ok := subs[socketID]
75+
if !ok {
76+
continue
77+
}
78+
delete(s.list[channel], socketID)
79+
fmt.Println("unsubscribe from " + channel)
80+
}
81+
}
82+
6883
// SendToAll sends a message to all sockets.
6984
func (s *Subscribers) SendToAll(buf []byte, channel string) *answer.Error {
7085
s.RLock()

0 commit comments

Comments
 (0)