示例#1
0
    public static void InitializePubSub()
    {
        if (isInitialized)
        {
            return;
        }

        isInitialized = true;
        //PlayFabSocketsAPI.Debugging = true;
        PlayFabSocketsAPI.Initialize();

        PlayFabSocketsAPI.OnConnected.AddListener(OnSocketsConnected);
        PlayFabSocketsAPI.Connect();
        PlayFabSocketsAPI.OnConnectionError.AddListener(OnSocketsConnectionError);
        PlayFabSocketsAPI.OnDisconnected.AddListener(OnSocketsDisconnected);
    }
示例#2
0
    private static void OnSocketsConnected()
    {
        // create topics for both title specific AND player specific messages
        var entity = new PlayFab.Sockets.Models.EntityKey()
        {
            Id   = currentEntity.Id,
            Type = currentEntity.Type
        };

        //Create a list of Topics to subscribe to
        var topics = new List <Topic>();

        //Create a Topic object to listen to
        var sendMessageToPlayerTopic = new Topic()
        {
            Entity   = entity,
            FullName = new PlayFab.Sockets.Models.EventFullName()
            {
                Name      = "MessageToPlayer",
                Namespace = "custom.UnicornBattle"
            }
        };

        //Add that topic to the array
        topics.Add(sendMessageToPlayerTopic);

        PlayFabSocketsAPI.RegisterHandler(sendMessageToPlayerTopic, OnReceiveMessageToPlayer);

        var sendMessageToAllPlayersTopic = new Topic()
        {
            Entity = new PlayFab.Sockets.Models.EntityKey()
            {
                Type = "Title",
                Id   = PlayFabSettings.TitleId
            },

            FullName = new PlayFab.Sockets.Models.EventFullName()
            {
                Name      = "MessageToAllPlayers",
                Namespace = "custom.UnicornBattle"
            }
        };

        //Add that topic to the array
        topics.Add(sendMessageToAllPlayersTopic);

        PlayFabSocketsAPI.RegisterHandler(sendMessageToAllPlayersTopic, OnReceiveMessageToPlayer);


        //send topic subscriptions and output any success or failures
        PlayFabSocketsAPI.Subscribe(topics, (subscribedTopics) =>
        {
            if (PlayFabSocketsAPI.Debugging)
            {
                Debug.Log("Subscribe Success");
            }

            /*          subscribedTopics.ForEach((t) =>
             *       {
             *           if (PlayFabSocketsAPI.Debugging)
             *               Debug.LogFormat("{0} Subscribed Successfully", t.EventName);
             *       }); */
        }, (subscribedErrors) =>
        {
            if (PlayFabSocketsAPI.Debugging)
            {
                Debug.Log("Subscribe Failed");
            }

            /*  subscribedErrors.ForEach((t) =>
             * {
             *   if (PlayFabSocketsAPI.Debugging)
             *       Debug.LogFormat("{0}", t.Message);
             * });		 */
        });
    }