/// <inheritdoc />
        public async Task OnGameInitialized()
        {
            //We need to connect the hub to the social backend
            ResolveServiceEndpointResponse endpointResponse = await ServiceDiscoveryService.DiscoverService(new ResolveServiceEndpointRequest(ClientRegionLocale.US, "SocialService"))
                                                              .ConfigureAwait(false);

            if (!endpointResponse.isSuccessful)
            {
                throw new InvalidOperationException($"Failed to query for SocialService. Reason: {endpointResponse.ResultCode}");
            }

            string hubConnectionString = $@"{endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort}/realtime/textchat";

            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Social HubConnection String: {hubConnectionString}");
            }

            //TODO: Handle failed service disc query
            HubConnection connection = new HubConnectionBuilder()
                                       .WithUrl(hubConnectionString, options =>
            {
                options.Headers.Add(SocialNetworkConstants.CharacterIdRequestHeaderName, PlayerDetails.LocalPlayerGuid.EntityId.ToString());
                options.AccessTokenProvider = () => Task.FromResult(AuthTokenProvider.Retrieve());
            })
                                       .AddJsonProtocol()
                                       .Build();

            foreach (var i in InitializableSocialServices)
            {
                i.Connection = connection;
            }

            //Just start the service when the game initializes
            //This will make it so that the signalR clients will start to recieve messages.
            await connection.StartAsync()
            .ConfigureAwait(false);
        }
Пример #2
0
        protected override void OnLocalPlayerSpawned(LocalPlayerSpawnedEventArgs args)
        {
            UnityAsyncHelper.UnityMainThreadContext.PostAsync(async() =>
            {
                try
                {
                    //We need to connect the hub to the social backend
                    ResolveServiceEndpointResponse endpointResponse = await ServiceDiscoveryService.DiscoverService(new ResolveServiceEndpointRequest(ClientRegionLocale.US, GladMMONetworkConstants.SOCIAL_SERVICE_NAME))
                                                                      .ConfigureAwaitFalse();

                    if (!endpointResponse.isSuccessful)
                    {
                        throw new InvalidOperationException($"Failed to query for SocialService. Reason: {endpointResponse.ResultCode}");
                    }

                    string hubConnectionString = $@"{endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort}/realtime/social";

                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Social HubConnection String: {hubConnectionString}");
                    }

                    //TODO: Handle failed service disc query
                    HubConnection connection = new HubConnectionBuilder()
                                               .WithUrl(hubConnectionString, options =>
                    {
                        options.Headers.Add(SocialNetworkConstants.CharacterIdRequestHeaderName, PlayerDetails.LocalPlayerGuid.EntityId.ToString());
                        options.AccessTokenProvider = () => Task.FromResult(AuthTokenProvider.Retrieve());
                    })
                                               .AddJsonProtocol()
                                               .Build();

                    //Register the reciever interface instance for the Connection Hub
                    connection.RegisterClientInterface <IRemoteSocialHubClient>(RemoteSocialClient);

                    foreach (var initable in ConnectionHubInitializable)
                    {
                        initable.Connection = connection;
                    }

                    //Just start the service when the game initializes
                    //This will make it so that the signalR clients will start to recieve messages.
                    await connection.StartAsync()
                    .ConfigureAwaitFalseVoid();

                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Connected to realtime Social Service.");
                    }

                    OnRealtimeSocialServiceConnected?.Invoke(this, EventArgs.Empty);
                }
                catch (Exception e)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Failed to connect to Social Service: {e.ToString()}");
                    }

                    throw;
                }
            });
        }