Notify new clients about current flags of virtual sessions.

This commit is contained in:
Joachim Bauch 2021-06-08 13:46:25 +02:00
parent 4d991725c3
commit 9d2ad0f243
No known key found for this signature in database
GPG Key ID: 77C1D22D53E15F02
2 changed files with 81 additions and 0 deletions

29
hub.go
View File

@ -1170,6 +1170,35 @@ func (h *Hub) notifyUserJoinedRoom(room *Room, session *ClientSession, sessionDa
// No need to send through NATS, the session is connected locally.
session.SendMessage(msg)
// Notify about initial flags of virtual sessions.
for _, s := range sessions {
vsess, ok := s.(*VirtualSession)
if !ok {
continue
}
flags := vsess.Flags()
if flags == 0 {
continue
}
msg := &ServerMessage{
Type: "event",
Event: &EventServerMessage{
Target: "participants",
Type: "flags",
Flags: &RoomFlagsServerMessage{
RoomId: room.Id(),
SessionId: vsess.PublicId(),
Flags: vsess.Flags(),
},
},
}
// No need to send through NATS, the session is connected locally.
session.SendMessage(msg)
}
}
}

View File

@ -197,6 +197,58 @@ func TestVirtualSession(t *testing.T) {
t.Errorf("Expected flags %d, got %+v", newFlags, flagsMsg.Flags)
}
// A new client will receive the initial flags of the virtual session.
client2 := NewTestClient(t, server, hub)
defer client2.CloseWithBye()
if err := client2.SendHello(testDefaultUserId + "2"); err != nil {
t.Fatal(err)
}
if _, err := client2.RunUntilHello(ctx); err != nil {
t.Error(err)
}
if room, err := client2.JoinRoom(ctx, roomId); err != nil {
t.Fatal(err)
} else if room.Room.RoomId != roomId {
t.Fatalf("Expected room %s, got %s", roomId, room.Room.RoomId)
}
gotFlags := false
var receivedMessages []*ServerMessage
for !gotFlags {
messages, err := client2.GetPendingMessages(ctx)
if err != nil {
t.Error(err)
}
receivedMessages = append(receivedMessages, messages...)
for _, msg := range messages {
if msg.Type != "event" || msg.Event.Target != "participants" || msg.Event.Type != "flags" {
continue
}
if msg.Event.Flags.RoomId != roomId {
t.Errorf("Expected flags in room %s, got %s", roomId, msg.Event.Flags.RoomId)
} else if msg.Event.Flags.SessionId != sessionId {
t.Errorf("Expected flags for session %s, got %s", sessionId, msg.Event.Flags.SessionId)
} else if msg.Event.Flags.Flags != newFlags {
t.Errorf("Expected flags %d, got %d", newFlags, msg.Event.Flags.Flags)
} else {
gotFlags = true
break
}
}
}
if !gotFlags {
t.Errorf("Didn't receive initial flags in %+v", receivedMessages)
}
// Ignore "join" messages from second client
if err := client.DrainMessages(ctx); err != nil {
t.Error(err)
}
// When sending to a virtual session, the message is sent to the actual
// client and contains a "Recipient" block with the internal session id.
recipient := MessageClientMessageRecipient{