Pages

Wednesday, September 16, 2015

SignalR with Client, Server modal

SignalR is a new developer's API provided for ASP.NET web applications, used to add "real time" web functionality to ASP.NET applications. "Real Time" web functionality is the ability to have server code to push contents to connected clients


SignalR supports "server push" or "broadcasting" functionality. It handles connection management automatically. In classic HTTP connections for client-server communication connection is re-established for each request, but SignalR provides persistent connection between the client and the server. In SignalR the server code calls out to a client code in the browser using Remote Procedure Calls (RPC), rather than request-response model today. SignalR is an open-source API, and is accessible through GitHub.

Where to use:
  1. Chat room applications
  2. Real-time monitoring applications
  3. Job progress updates
  4. Real time forms
Web Client

    Server end
     class Program
        {
            static void Main(string[] args)
            {
                // This will *ONLY* bind to localhost, if you want to bind to all addresses
                // use http://*:8080 to bind to all addresses. 
                // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
                // for more information.
                string url = "http://localhost:8080";
                WebApp.Start(url);
                Console.WriteLine("Server running on {0} \n", url);
                string command;
                Boolean quitNow = false;
                while (!quitNow)
                {
                    Console.WriteLine("Enter message for clients: ");
                    command = Console.ReadLine();
                    switch (command)
                    {
                        case "/quit":
                            quitNow = true;
                            break;
    
                        default:
                            var context = GlobalHost.ConnectionManager.GetHubContext();
                            context.Clients.All.NotifyMsg("server notifcation", command);
                            break;
                    }
                }
    
            }
        }
    
    
        class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseCors(CorsOptions.AllowAll);
                app.MapSignalR();
            }
        }
        public class MyHub : Hub
        {
            public void Send(string name, string message)
            {
                Clients.All.NotifyMsg(name, message);
            }
        }
    

    1 comment: