public StormancerRouteViewModel(Route route) { _route = route; }
public IObservable<Packet<IScenePeer>> OnMessage(Route route) { // var index = route.Handle; var observable = Observable.Create<Packet<IScenePeer>>(observer => { Action<Packet> action = (data) => { var packet = new Packet<IScenePeer>(Host, data.Stream, data.Metadata); observer.OnNext(packet); }; route.Handlers += action; return () => { route.Handlers -= action; }; }); return observable; }
/// <summary> /// Creates an IObservable<Packet> instance that listen to events on the specified route. /// </summary> /// <param name="route">A string containing the name of the route to listen to.</param> /// <returns type="IObservable<Packet>">An IObservable<Packet> instance that fires each time a message is received on the route. </returns> public IObservable<Packet<IScenePeer>> OnMessage(string route) { if (Connected) { DependencyResolver.GetComponent<ILogger>().Error("Tried rgister handles once connected"); throw new InvalidOperationException("You cannot register handles once the scene is connected."); } Route routeObj; if (!_localRoutesMap.TryGetValue(route, out routeObj)) { routeObj = new Route(this, route, new Dictionary<string, string>()); _localRoutesMap.Add(route, routeObj); } return OnMessage(routeObj); }
/// <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] == '@') { DependencyResolver.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) { DependencyResolver.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)) { DependencyResolver.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); }