Skip to content
davidfowl edited this page Jun 27, 2012 · 9 revisions

Using SignalR Selfhost

The default SelfHost implementation is built on HttpListener and can be hosted in any kind of application (Console, Windows Service etc).

Getting Started

To get started, install the self host package:

Install-Package SignalR.Hosting.Self

Full Sample - Persistent Connection

using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace SignalR.Hosting.Self.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8081/";
            var server = new Server(url);
            
            // Map connections
            server.MapConnection<MyConnection>("/echo");

            // Start the server
            server.Start();

            Console.WriteLine("Server running on {0}", url);

            // Keep going until somebody hits 'x'
            while (true)
            {
                ConsoleKeyInfo ki = Console.ReadKey(true);
                if (ki.Key == ConsoleKey.X)
                {
                    break;
                }
            }
        }

        public class MyConnection : PersistentConnection
        {
            protected override Task OnConnectedAsync(IRequest request, string connectionId)
            {
                Console.WriteLine("{0} connected", connectionId);
                return base.OnConnectedAsync(request, connectionId);
            }

            protected override Task OnReceivedAsync(IRequest request, string connectionId, string data)
            {
                return Connection.Broadcast(data);
            }

            protected override Task OnDisconnectAsync(string connectionId)
            {
                Console.WriteLine("{0} left", connectionId);
                return base.OnDisconnectAsync(connectionId);
            }
        }
    }
}

Full Sample - Hubs

using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace SignalR.Hosting.Self.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8081/";
            var server = new Server(url);
            
            // Map the default hub url (/signalr)
            server.MapHubs();

            // Start the server
            server.Start();

            Console.WriteLine("Server running on {0}", url);

            // Keep going until somebody hits 'x'
            while (true)
            {
                ConsoleKeyInfo ki = Console.ReadKey(true);
                if (ki.Key == ConsoleKey.X)
                {
                    break;
                }
            }
        }

        public class MyHub : Hub
        {
            public void Send(string message)
            {
                Clients.addMessage(message);
            }
        }
    }
}

Javascript client

The JS SignalR Hubs script supports cross-domain requests, allowing a self-hosted server to be consumed from a JS client by setting the url property on the connection before calling start(). For more information, please see the section on Cross Domain Support in SignalR JS Client Hubs.

Clone this wiki locally