onDisconnectDone - Can only be called from the main thread...

0 votes

I can not call methods (for example SendMessage) from onDisconnectDone. I always get an exception like this: Can only be called from the main thread ... There is no such problem on onConnectDone. A small sample code (Unity 2017.4.0f1):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using com.shephertz.app42.gaming.multiplayer.client;
using com.shephertz.app42.gaming.multiplayer.client.events;
using com.shephertz.app42.gaming.multiplayer.client.command;
using com.shephertz.app42.gaming.multiplayer.client.listener;

public class WarpTest : MonoBehaviour, ConnectionRequestListener
{

	public string apiKey = "<apiKey>";
	public string pvtKey = "<pvtKey>";

	void OnWarpConnect ()
	{
		print ("Warp Connect!");
	}

	void OnWarpDisconnect ()
	{
		print ("Warp Disconnect!");
	}
	
	public void onConnectDone (ConnectEvent eventObj) 
	{
		this.SendMessage ("OnWarpConnect", null, SendMessageOptions.DontRequireReceiver);
	}

	public void onDisconnectDone (ConnectEvent eventObj)
	{
		this.SendMessage ("OnWarpDisconnect", null, SendMessageOptions.DontRequireReceiver);
		// ERROR!
	}

	public void onInitUDPDone (byte resultCode) { }

	// Use this for initialization
	void Awake () {
		WarpClient.initialize (this.apiKey, this.pvtKey);
		WarpClient.GetInstance ().AddConnectionRequestListener (this);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown (KeyCode.C)) WarpClient.GetInstance ().Connect ("Player");
		if (Input.GetKeyDown (KeyCode.D)) WarpClient.GetInstance ().Disconnect ();
	}
}

 

asked Jul 22 in Unity by perchik (10 points)
edited Jul 22 by perchik
I have forwarded this to my development team to look into it. I will update you as soon as possible.
Good. Thank you! I will wait.

1 Answer

0 votes
Hi,

You need to call sendMessage on main thread as onDisconnectDone is not fired on main thread.

Let me know if you have any further query, I will be happy to help.

Thanks.
answered Jul 30 by rajeev.etc (1,631 points)
Thank you. I decided to use the events. Now there are no exceptions.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...