/// <summary> /// Registers a route on the local peer. /// </summary> /// <param name="route">A string containing the name of the route to listen to.</param> /// <param name="handler">An action that is executed when the remote peer call the route.</param> /// <returns></returns> public void AddRoute(string route, Action <Packet <IScenePeer> > handler, Dictionary <string, string> metadata = null) { if (route[0] == '@') { resolver.GetComponent <ILogger>().Log(Stormancer.Diagnostics.LogLevel.Error, this.Id, "AddRoute failed: Tried to create a route with the @ character"); throw new ArgumentException("A route cannot start with the @ character."); } metadata = new Dictionary <string, string>(); if (Connected) { resolver.GetComponent <ILogger>().Error("AddRoute failed: Tried to create a route once connected"); throw new InvalidOperationException("You cannot register handles once the scene is connected."); } Route routeObj; if (!_localRoutesMap.TryGetValue(route, out routeObj)) { resolver.GetComponent <ILogger>().Trace("Created route with id : '{0}'", route); routeObj = new Route(this, route, metadata); _localRoutesMap.Add(route, routeObj); var ev = _pluginCtx.RouteCreated; if (ev != null) { ev(this, routeObj); } } OnMessage(route).Subscribe(handler); }
public Task <SceneEndpoint> GetSceneEndpoint <T>(string accountId, string applicationName, string sceneId, T userData) { var serializer = new MsgPackSerializer(); byte[] data; using (var s = new MemoryStream()) { serializer.Serialize(userData, s); data = s.ToArray(); } var logger = _resolver.GetComponent <ILogger>(); logger.Log(Stormancer.Diagnostics.LogLevel.Trace, "Client", "creating endpoint request for remote server"); var uri = new Uri(_config.GetApiEndpoint(), string.Format(CreateTokenUri, accountId, applicationName, sceneId)); var request = new Request("POST", uri.AbsoluteUri, data); request.AddHeader("Content-Type", "application/msgpack"); request.AddHeader("Accept", "application/json"); request.AddHeader("x-version", "1.0.0"); logger.Trace("Sending endpoint request to remote server"); return(SendWithRetry(request, 5000, 15000).ContinueWith(t => { logger.Log(Stormancer.Diagnostics.LogLevel.Trace, "Client", "Received endpoint response from remote server"); try { var response = t.Result; try { response.EnsureSuccessStatusCode(); } catch (HTTPException exception) { logger.Log(Stormancer.Diagnostics.LogLevel.Error, "Client", "GetScene failed."); if (exception.StatusCode == HttpStatusCode.NotFound) { logger.Log(Stormancer.Diagnostics.LogLevel.Error, "Client", "GetScene failed: Unable to get the scene. Please check you entered the correct account id, application name and scene id."); throw new ArgumentException("Unable to get the scene {0}/{1}/{2}. Please check you entered the correct account id, application name and scene id.", exception); } throw; } logger.Log(Stormancer.Diagnostics.LogLevel.Trace, "Client", "Token succefully received"); return _resolver.GetComponent <ITokenHandler>().DecodeToken(response.ReadAsString()); } catch (Exception ex) { UnityEngine.Debug.LogException(ex); logger.Log(Stormancer.Diagnostics.LogLevel.Error, "Client", "GetScene failed: cannot retreive the connection token."); throw new InvalidOperationException("An error occured while retrieving the connection token. See the inner exception for more informations.", ex); } })); }