public static IEnumerator Start(Action callback) { // First, check if user has location service enabled if (!Input.location.isEnabledByUser) { MatchmoreLogger.Debug("Location service disabled by user"); #if !UNITY_IOS //https://docs.unity3d.com/ScriptReference/LocationService-isEnabledByUser.html //if it is IOS we do not break here yield break; #endif } // Start service before querying location Input.location.Start(); // Wait until service initializes int maxWait = 20; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { yield return(new WaitForSeconds(1)); maxWait--; } // Service didn't initialize in 20 seconds if (maxWait < 1) { yield break; } // Connection has failed if (Input.location.status == LocationServiceStatus.Failed) { yield break; } callback(); }
public WebsocketMatchMonitor(Device device, Matchmore.Config config, DeviceApi deviceApi, CoroutineWrapper coroutine, Action<string> closedCallback) { if (device == null || string.IsNullOrEmpty(device.Id)) { throw new ArgumentException("Device null or invalid id"); } _device = device; _deviceApi = deviceApi; _coroutine = coroutine; _closedCallback = closedCallback; var worldId = Utils.ExtractWorldId(config.ApiKey); MatchmoreLogger.Debug("Starting websocket for device {0}", device.Id); var protocol = config.UseSecuredCommunication ? "wss" : "ws"; var port = config.PusherPort == null ? "" : ":" + config.PusherPort; var url = string.Format("{3}://{0}{4}/pusher/{1}/ws/{2}", config.Environment, Matchmore.API_VERSION, _device.Id, protocol, port); _ws = new WebSocket(url, "api-key", worldId); _ws.OnOpen += (sender, e) => MatchmoreLogger.Debug("WS opened for device {0}", device.Id); _ws.OnClose += (sender, e) => MatchmoreLogger.Debug("WS closing {0} for device {1}", e.Code, device.Id); _ws.OnError += (sender, e) => MatchmoreLogger.Debug("Error in WS {0} for device {1}", e.Message, device.Id); _ws.OnMessage += (sender, e) => { var data = e.Data; if (data == "ping") { _ws.Send("pong"); } else { _coroutine.RunOnce(Id, GetMatch(data)); } }; _ws.Connect(); }