pusher / pusher-websocket-unity

Pusher Channels Unity Client Library
MIT License
27 stars 20 forks source link

Pusher becomes broken in my project #60

Open Suppzs opened 2 months ago

Suppzs commented 2 months ago

Hello, I'm unable to run pusher in the scenes of my project, it's able to run in blank scenes though, it just doesn't seem to trigger the events, all of them. I still can see it being connected in the dashboard, but yet I'm unable to interact with it at all

benw-pusher commented 2 months ago

Can you share more details of how you are attempting to interact with it and any errors you are encountering? Code snippets would also be helpful - does the same code work in a blank scene but fail in a populated scene?

Suppzs commented 2 months ago

Can you share more details of how you are attempting to interact with it and any errors you are encountering? Code snippets would also be helpful - does the same code work in a blank scene but fail in a populated scene?

well, there is a gameobject with only pushermanager script attached to it. There are no errors or yet any output in the console. Yes it does fail in my scenes, but succeeds to run in empty scenes, also I tried running it in the empty scene and then switching to the populated one and it died after.

script I was using

using System;
using System.Threading.Tasks;
using PusherClient;
using UnityEngine;

public class PusherManager : MonoBehaviour
{
    public static PusherManager instance = null;
    private Pusher _pusher;
    private Channel _channel;
    private const string APP_KEY = "";
    private const string APP_CLUSTER = "";

    async Task Start()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
        DontDestroyOnLoad(gameObject);
        await InitialisePusher();
        Console.WriteLine("Starting");
    }

    private async Task InitialisePusher()
    {
        if (_pusher == null && (APP_KEY != "APP_KEY") && (APP_CLUSTER != "APP_CLUSTER"))
        {
            _pusher = new Pusher(APP_KEY, new PusherOptions()
            {
                Cluster = APP_CLUSTER,
                Encrypted = true
            });

            _pusher.Error += OnPusherOnError;
            _pusher.ConnectionStateChanged += PusherOnConnectionStateChanged;
            _pusher.Connected += PusherOnConnected;
            _channel = await _pusher.SubscribeAsync("my-channel");
            _pusher.Subscribed += OnChannelOnSubscribed;
            await _pusher.ConnectAsync();
        }
        else
        {
            Debug.LogError("APP_KEY and APP_CLUSTER must be correctly set. Find how to set it at https://dashboard.pusher.com");
        }
    }

    private void PusherOnConnected(object sender)
    {
        Debug.Log("Connected");
        _channel.Bind("my-event", (dynamic data) =>
        {
            Debug.Log("my-event received");
        });
    }

    private void PusherOnConnectionStateChanged(object sender, ConnectionState state)
    {
        Debug.Log("Connection state changed");
    }

    private void OnPusherOnError(object s, PusherException e)
    {
        Debug.Log("Errored");
    }

    private void OnChannelOnSubscribed(object s, Channel channel)
    {
        Debug.Log("Subscribed");
    }

    async Task OnApplicationQuit()
    {
        if (_pusher != null)
        {
            await _pusher.DisconnectAsync();
        }
    }
}