Implementing PlayFab's lobby notifications using SignalR

theo 1 Reputation point
2022-05-23T10:22:12.71+00:00

Hi,

I'm trying to implement PlayFab's lobby notifications which relies on SignalR.
One of the things I have to do to start receving notifications from the SignalR service is to start the session by calling a server method (signalr-hub start-or-recover-session).
Here is how my code looks like at the moment :

public struct StartOrRecoverSessionResponse  
{  
    public string newConnectionHandle;  
    public string[] recoveredTopics;  
    public string status;  
    public string traceId;  
}  
  
public struct StartOrRecoverSessionArgs  
{  
    public string oldConnectionHandle;  
    public string traceParent;  
}  
  
...  
  
_connection = new HubConnectionBuilder()  
    .WithUrl("https://mytitleid.playfabapi.com/pubsub", ConfigureHttpConnection)  
    .ConfigureLogging((builder) =>  
    {  
        builder.AddProvider(new SignalRLoggerProvider());  
    })  
    .Build();  
  
_connection.On<string>("ReceiveMessage", (message) =>  
{  
    Debug.Log($"ReceivedMessage {message}");  
});  
  
_connection.On<string>("ReceiveSubscriptionChangeMessage", (message) =>  
{  
    Debug.Log($"ReceiveSubscriptionChangeMessage {message}");  
});  
  
try  
{  
    await _connection.StartAsync();  
  
    var response = await _connection.InvokeAsync<StartOrRecoverSessionResponse>("StartOrRecoverSession", new StartOrRecoverSessionArgs() {  
        oldConnectionHandle = "",  
        traceParent = "00-84678fd69ae13e41fce1333289bcf482-22d157fb94ea4827-01"  
    });  
  
    Debug.Log("Connection Handle " + response.ToString());  
}  
catch (Exception ex)  
{  
    Debug.LogError(ex.Message);  
}  
  
  

It doesn't trigger any exception but the returned struct is always filled with null strings.

StartOrRecoverSessionArgs and StartOrRecoverSessionResponse are structs that I created by following StartOrRecoverSession's documentation.

I originally posted on PlayFab forums at implementing-lobby-notifications-with-c-and-micros.html

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,510 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
147 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,026 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sergo Martirosov 0 Reputation points
    2023-07-27T08:30:08.4466667+00:00

    If you encounter the same issue, try making the following modification to the StartOrRecoverSessionResponse to resolve the problem:

    [Serializable]
    public class StartOrRecoverSessionResponse
    {
        [Newtonsoft.Json.JsonProperty("newConnectionHandle")]
        public string NewConnectionHandle { get; set; }
    
        [Newtonsoft.Json.JsonProperty("recoveredTopics")]
        public string[] RecoveredTopics { get; set; }
    
        [Newtonsoft.Json.JsonProperty("status")]
        public string Status { get; set; }
    
        [Newtonsoft.Json.JsonProperty("traceId")]
        public string TraceId { get; set; }
    
        public StartOrRecoverSessionResponse(string newConnectionHandle, string[] recoveredTopics, string status, string traceId)
        {
            this.NewConnectionHandle = newConnectionHandle;
            this.RecoveredTopics = recoveredTopics;
            this.Status = status;
            this.TraceId = traceId;
        }
    }
    
    
            // Initialize the SignalR connection
            connection = new HubConnectionBuilder()
                .WithUrl("https://xxxxx.playfabapi.com/PubSub", options => { options.Headers.Add("X-EntityToken", entityToken); })
                .WithAutomaticReconnect()
                .Build();
    
    // Start the SignalR connection
    await connection.StartAsync();
                
    var response = await connection.InvokeAsync<StartOrRecoverSessionResponse>("StartOrRecoverSession", new StartOrRecoverSessionRequest { traceParent = "00-84678fd69ae13e41fce1333289bcf482-22d157fb94ea4827-01" });
                
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.