TwitchLib / TwitchLib.Unity

TwitchLib repository representing all code belonging to the implementation of TwitchLib for Unity. Maintained primarily by LuckyNoS7evin.
164 stars 33 forks source link

Pubsub example not working with Release 1.0.5, hangs when calling .Connect() #73

Open Nights87 opened 7 months ago

Nights87 commented 7 months ago

Upon running the line _pubSub.Connect(); unity just hangs and I have to kill the editor. Running 2023.2.4f1 with HDRP pipeline.

Is there anything I need to add/do?

Code is same as the sample, but ill post anyway

private void Start()
{
  // Create new instance of PubSub Client
  _pubSub = new PubSub();

  // Subscribe to Events
  _pubSub.OnWhisper += OnWhisper;
  _pubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;

  // Connect - hangs when running this
  _pubSub.Connect();
}
ffortat commented 3 months ago

I had the same issue, it appears all network related methods in TwitchLib.PubSub has been updated to use Async methods (see this commit)

From what I've found, running async code on Unity's main thread freezes it, you have to wrap it in a Task.Run(...) so you can change your snippet to this:

private void Start()
{
  // Create new instance of PubSub Client
  _pubSub = new PubSub();

  // Subscribe to Events
  _pubSub.OnWhisper += OnWhisper;
  _pubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;

  // Connect - hangs when running this
  Task.Run(async() => await _pubSub.ConnectAsync());
}

This way it shouldn't hang. The same goes for all the methods that now have an Async variant.

I don't know if it's the best way to go, it would be nice to update this lib to have a helper to run in coroutines for example, but this works for now.