Get userid from room session data if present.
This commit is contained in:
parent
047e6655a1
commit
3a2adffab6
|
@ -240,7 +240,15 @@ func (s *ClientSession) ParsedBackendUrl() *url.URL {
|
|||
}
|
||||
|
||||
func (s *ClientSession) UserId() string {
|
||||
return s.userId
|
||||
userId := s.userId
|
||||
if userId == "" {
|
||||
if room := s.GetRoom(); room != nil {
|
||||
if data := room.GetRoomSessionData(s); data != nil {
|
||||
userId = data.UserId
|
||||
}
|
||||
}
|
||||
}
|
||||
return userId
|
||||
}
|
||||
|
||||
func (s *ClientSession) UserData() *json.RawMessage {
|
||||
|
|
10
hub_test.go
10
hub_test.go
|
@ -252,6 +252,16 @@ func processRoomRequest(t *testing.T, w http.ResponseWriter, r *http.Request, re
|
|||
RoomId: request.Room.RoomId,
|
||||
},
|
||||
}
|
||||
if request.Room.RoomId == "test-room-with-sessiondata" {
|
||||
data := map[string]string{
|
||||
"userid": "userid-from-sessiondata",
|
||||
}
|
||||
tmp, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not marshal %+v: %s", data, err)
|
||||
}
|
||||
response.Room.Session = (*json.RawMessage)(&tmp)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
|
|
10
room.go
10
room.go
|
@ -354,6 +354,12 @@ func (r *Room) UpdateProperties(properties *json.RawMessage) {
|
|||
r.publish(message)
|
||||
}
|
||||
|
||||
func (r *Room) GetRoomSessionData(session Session) *RoomSessionData {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
return r.roomSessionData[session.PublicId()]
|
||||
}
|
||||
|
||||
func (r *Room) PublishSessionJoined(session Session, sessionData *RoomSessionData) {
|
||||
sessionId := session.PublicId()
|
||||
if sessionId == "" {
|
||||
|
@ -617,8 +623,8 @@ func (r *Room) publishSessionFlagsChanged(session *VirtualSession) {
|
|||
}
|
||||
|
||||
func (r *Room) publishActiveSessions() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
entries := make(map[string][]BackendPingEntry)
|
||||
urls := make(map[string]*url.URL)
|
||||
|
|
57
room_test.go
57
room_test.go
|
@ -355,3 +355,60 @@ loop:
|
|||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoom_RoomSessionData(t *testing.T) {
|
||||
hub, _, router, server, shutdown := CreateHubForTest(t)
|
||||
defer shutdown()
|
||||
|
||||
config, err := getTestConfig(server)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := NewBackendServer(config, hub, "no-version")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := b.Start(router); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
client := NewTestClient(t, server, hub)
|
||||
defer client.CloseWithBye()
|
||||
|
||||
if err := client.SendHello(authAnonymousUserId); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
|
||||
defer cancel()
|
||||
|
||||
hello, err := client.RunUntilHello(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Join room by id.
|
||||
roomId := "test-room-with-sessiondata"
|
||||
if room, err := client.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)
|
||||
}
|
||||
|
||||
// We will receive a "joined" event with the userid from the room session data.
|
||||
expected := "userid-from-sessiondata"
|
||||
if message, err := client.RunUntilMessage(ctx); err != nil {
|
||||
t.Error(err)
|
||||
} else if err := client.checkMessageJoinedSession(message, hello.Hello.SessionId, expected); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
session := hub.GetSessionByPublicId(hello.Hello.SessionId)
|
||||
if session == nil {
|
||||
t.Fatalf("Could not find session %s", hello.Hello.SessionId)
|
||||
}
|
||||
|
||||
if userid := session.UserId(); userid != expected {
|
||||
t.Errorf("Expected userid %s, got %s", expected, userid)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue