Use OWIN to Self-Host ASP.NET Web API and SignalR

Startup.cs

[assembly: OwinStartup(typeof(ERPSelfHostServer.Startup))]
namespace ERPSelfHostServer
{
    public class Startup
    {
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.Formatters.Add(new BrowserJsonFormatter());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );


            // Any connection or hub wire up and configuration should go here
            appBuilder.MapSignalR();

            appBuilder.UseWebApi(config);
            
        }
    }
}

Program.cs

static void Main(string[] args)
        {
            // bind to all network interfaces
            //string baseAddress = "http://*:13602/";
            string baseAddress = "http://172.20.63.161:13602";

            using (WebApp.Start<Startup>(baseAddress))
            {
                Thread.Sleep(Timeout.Infinite);
            }
        }

References
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
http://anthonychu.ca/post/web-api-owin-self-host-docker-windows-containers/
http://stackoverflow.com/questions/21634333/hosting-webapi-using-owin-in-a-windows-service
http://stackoverflow.com/questions/20068075/owin-startup-class-missing
http://stackoverflow.com/questions/16642651/self-hosted-owin-and-urlacl

Getting Started with SignalR 2 and MVC 5

Server
Right-click the Hubs folder, click Add | New Item, select the Visual C# | Web | SignalR node in the Installed pane, select SignalR Hub Class (v2) from the center pane, and create a new hub named ChatHub.cs. You will use this class as a SignalR server hub that sends messages to all clients

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the addNewMessageToPage method to update clients.
            Clients.All.addNewMessageToPage(name, message);
        }
    }
}

Create a new class called Startup.cs. Change the contents of the file to the following

using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

Client

<!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.  
            var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + htmlEncode(name) 
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>

References
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc

How to connect to a SignalR hub from PhoneGap or Apache Cordova

<script type="text/javascript">
    $(function () {
        $.connection.hub.url = "http://jgough/SignalR/signalr";

        // Grab the hub by name, the same name as specified on the server
        var chat = $.connection.chat;

        chat.addMessage = function (message) {
            $('#chatMessages').append('<li>' + message + '</li>');
        };

        $.connection.hub.start({ jsonp: true });

        $("#sendChatMessage").click(function () {
            var message = $("#chatMessage").val();
            console.log("Message: " + message);
            chat.send(message);
        });
    });
</script>

References
http://stackoverflow.com/questions/16501590/how-to-connect-to-a-signalr-hub-from-phonegap-app-on-ios