Browse Source

Merge pull request #121 from strukturag/initial-virtual-flags

Notify new clients about flags of virtual sessions
master
Joachim Bauch 2 weeks ago
committed by GitHub
parent
commit
b091db000e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      hub.go
  2. 26
      testclient_test.go
  3. 52
      virtualsession_test.go

29
hub.go

@ -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)
}
}
}

26
testclient_test.go

@ -422,6 +422,32 @@ func (c *TestClient) DrainMessages(ctx context.Context) error {
return nil
}
func (c *TestClient) GetPendingMessages(ctx context.Context) ([]*ServerMessage, error) {
var result []*ServerMessage
select {
case err := <-c.readErrorChan:
return nil, err
case msg := <-c.messageChan:
var m ServerMessage
if err := json.Unmarshal(msg, &m); err != nil {
return nil, err
}
result = append(result, &m)
n := len(c.messageChan)
for i := 0; i < n; i++ {
var m ServerMessage
msg = <-c.messageChan
if err := json.Unmarshal(msg, &m); err != nil {
return nil, err
}
result = append(result, &m)
}
case <-ctx.Done():
return nil, ctx.Err()
}
return result, nil
}
func (c *TestClient) RunUntilMessage(ctx context.Context) (message *ServerMessage, err error) {
select {
case err = <-c.readErrorChan:

52
virtualsession_test.go

@ -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{

Loading…
Cancel
Save