Exemplo n.º 1
0
 public WebSocketTransport(HostContext context, 
                           IJsonSerializer serializer, 
                           ITransportHeartBeat heartBeat)
     : base(context, serializer, heartBeat)
 {
     _context = context;
 }
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            var hub = ((IHub)this);

            var hubName = this.GetType().FullName;
            var hubManager = _resolver.Resolve<IHubManager>();
            var descriptor = hubManager.EnsureHub(hubName);

            var user = controllerContext.Request.GetUserPrincipal();

            // Response parameter is null here because outgoing broadcast messages will always go
            // via the SignalR intrinsics, and method return values via the Web API intrinsics.
            var hostContext = new HostContext(new WebApiRequest(controllerContext.Request), null, user);
            var connectionId = hostContext.Request.QueryString["connectionId"];
            hub.Context = new HubContext(hostContext, connectionId);

            var connection = _resolver.Resolve<IConnectionManager>().GetConnection<HubDispatcher>();
            var state = new TrackingDictionary();
            var agent = new ClientAgent(connection, descriptor.Name);
            hub.Caller = new SignalAgent(connection, connectionId, descriptor.Name, state);
            hub.Agent = agent;
            hub.GroupManager = agent;

            base.Initialize(controllerContext);
        }
Exemplo n.º 3
0
 public WebSocketTransport(HostContext context,
                           IDependencyResolver resolver)
     : this(context, 
            resolver.Resolve<IJsonSerializer>(),
            resolver.Resolve<ITransportHeartBeat>())
 {
 }
Exemplo n.º 4
0
 public HubContext(HostContext context, string connectionId)
 {
     ConnectionId = connectionId;
     Cookies = context.Request.Cookies;
     Headers = context.Request.Headers;
     User = context.User;
 }
Exemplo n.º 5
0
 public HubCallerContext(HostContext context, string connectionId)
 {
     ConnectionId = connectionId;
     RequestCookies = context.Request.Cookies;
     Headers = context.Request.Headers;
     QueryString = context.Request.QueryString;
     User = context.User;
 }
 public TransportDisconnectBase(HostContext context, IJsonSerializer jsonSerializer, ITransportHeartBeat heartBeat)
 {
     _context = context;
     _jsonSerializer = jsonSerializer;
     _heartBeat = heartBeat;
     _timeoutTokenSource = new CancellationTokenSource();
     _hostShutdownToken = context.HostShutdownToken();
 }
        public FleckWebSocketTransport(HostContext context, IJsonSerializer serializer)
        {
            _context = context;
            _serializer = serializer;
            _webSocketConnection = context.GetValue<IWebSocketConnection>("Fleck.IWebSocketConnection");

            // Groups never come from the client
            Groups = Enumerable.Empty<string>();
        }
Exemplo n.º 8
0
        public TransportDisconnectBase(HostContext context, ITransportHeartBeat heartBeat)
        {
            _context = context;
            _heartBeat = heartBeat;
            _timeoutTokenSource = new CancellationTokenSource();

            // Register the callback to cancel this connection
            var hostShutdownToken = context.HostShutdownToken();
            if (hostShutdownToken != CancellationToken.None)
            {
                hostShutdownToken.Register(_timeoutTokenSource.Cancel);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets the specified transport for the specified <see cref="HostContext"/>.
        /// </summary>
        /// <param name="hostContext">The <see cref="HostContext"/> for the current request.</param>
        /// <returns>The <see cref="ITransport"/> for the specified <see cref="HostContext"/>.</returns>
        public ITransport GetTransport(HostContext context)
        {
            string transportName = context.Request.QueryString["transport"];

            if (String.IsNullOrEmpty(transportName))
            {
                return null;
            }

            Func<HostContext, ITransport> factory;
            if (_transports.TryGetValue(transportName, out factory))
            {
                return factory(context);
            }

            return null;
        }
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var containsSignalRHubsRoute = routes.ContainsKey("signalr.hubs");

            if (!containsSignalRHubsRoute)
            {
                return base.SendAsync(request, cancellationToken);
            }

            var signalRHubRoute = routes["signalr.hubs"];
            var isSignalRRequest = request.RequestUri.LocalPath.Contains(signalRHubRoute.RouteTemplate.Split('/')[0]);

            if (isSignalRRequest)
            {
                var tcs = new TaskCompletionSource<HttpResponseMessage>();
                var req = new WebApiRequest(request);
                var response = new HttpResponseMessage();
                var resp = new WebApiResponse(cancellationToken, response, () => tcs.TrySetResult(response));
                var host = new HostContext(req, resp, Thread.CurrentPrincipal);

                connection.Initialize(resolver);
                connection.ProcessRequestAsync(host).ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        tcs.TrySetException(task.Exception);
                    }
                    else if (task.IsCanceled)
                    {
                        tcs.TrySetCanceled();
                    }
                    else
                    {
                        tcs.TrySetResult(response);
                    }
                });

                return tcs.Task;
            }
                
            return base.SendAsync(request, cancellationToken);
        }
Exemplo n.º 11
0
 public ForeverTransport(HostContext context, IJsonSerializer jsonSerializer, ITransportHeartBeat heartBeat)
     : base(context, heartBeat)
 {
     _jsonSerializer = jsonSerializer;
 }
Exemplo n.º 12
0
 public LongPollingTransport(HostContext context, IJsonSerializer jsonSerializer, ITransportHeartBeat heartBeat)
     : base(context, heartBeat)
 {
     _jsonSerializer = jsonSerializer;
 }
 public ServerSentEventsTransport(HostContext context, IDependencyResolver resolver)
     : base(context, resolver)
 {
 }
Exemplo n.º 14
0
 private static ITransport GetFleckWebSocketTransport(IDependencyResolver resolver, HostContext hostContext)
 {
     var serializer = resolver.Resolve<IJsonSerializer>();
     return new FleckWebSocketTransport(hostContext, serializer);
 }
Exemplo n.º 15
0
 public TransportDisconnectBase(HostContext context, ITransportHeartBeat heartBeat)
 {
     _context = context;
     _heartBeat = heartBeat;
 }
Exemplo n.º 16
0
 public ForeverFrameTransport(HostContext context, IDependencyResolver resolver)
     : base(context, resolver)
 {
     _isDebug = context.IsDebuggingEnabled();
 }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            Debug.Listeners.Add(new ConsoleTraceListener());
            Debug.AutoFlush = true;
            FleckLog.Level = LogLevel.Debug;

            // Web socket server
            var wss = new WebSocketServer("ws://*****:*****@"..\..\www");

            // Hijack the negotiation request
            server.OnProcessRequest = hostContext =>
            {
                // The server supports websockets
                hostContext.Items[HostConstants.SupportsWebSockets] = true;

                // In negotiation, we tell the client the url of the web socket server for this connection
                hostContext.Items[HostConstants.WebSocketServerUrl] = wss.Location + hostContext.Request.Url.LocalPath.Replace("/negotiate", "");
            };

            wss.Start(socket =>
            {
                PersistentConnection connection;
                if (server.TryGetConnection(socket.ConnectionInfo.Path, out connection))
                {
                    // Initalize the connection
                    connection.Initialize(server.DependencyResolver);

                    var req = new FleckWebSocketRequest(socket.ConnectionInfo, wss.IsSecure);
                    var hostContext = new HostContext(req, null, null);

                    // Stack the socket in the items collection so the transport can use it
                    hostContext.Items["Fleck.IWebSocketConnection"] = socket;

                    try
                    {
                        connection.ProcessRequestAsync(hostContext).ContinueWith(task =>
                        {
                            Console.WriteLine(task.Exception.GetBaseException());
                        },
                        TaskContinuationOptions.OnlyOnFaulted);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                else
                {
                    socket.Close();
                }
            });


            // HACK: Need to make it easier to plug this in cleaner
            var transportManager = (TransportManager)server.DependencyResolver.Resolve<ITransportManager>();

            // Register the websocket transport
            transportManager.Register("webSockets", context => GetFleckWebSocketTransport(server.DependencyResolver, context));

            server.MapConnection<Raw>("/raw");
            server.EnableHubs();

            server.Start();
            fileServer.Start();

            Process.Start("http://localhost:8081/public/raw/index.htm");

            Console.ReadKey();

            server.Stop();
            fileServer.Stop();
        }