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);
            }
        }
    

    Monday, September 14, 2015

    Behavioral Pattern - Command Pattern

    Command pattern encapsulates a request as an object and gives it a known public interface. Command Pattern ensures that every object receives its own commands and provides a decoupling between sender and receiver. A sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.


    Command Pattern

      public class GarageDoor
        {
            public string Up()
            {
                return "Garage Door Is Open";
            }
    
            public string Down()
            {
                return "Garage Door Is Closed";
            }
    
            public string Stop()
            {
                return "Stop door";
            }
    
            public string LightOn()
            {
                return "light On";
            }
    
            public string LightOff()
            {
                return "light Off";
            }
        }
    
        public class Light
        {
            public string On()
            {
                return "Light is on";
            }
        }
    
        public interface Command
        {
            string execute();
        }
    
        public class GarageDoorOpenCommand : Command
        {
            GarageDoor objGarageDoor = new GarageDoor();
            public GarageDoorOpenCommand(GarageDoor tmpGarageDoor)
            {
                this.objGarageDoor = tmpGarageDoor;
            }
    
            public string execute()
            {
                return this.objGarageDoor.Up();
            }
        }
    
        public class GarageDoorClosedCommand : Command
        {
            GarageDoor garagedoor;
            public GarageDoorClosedCommand(GarageDoor tempGarageDoor)
            {
                this.garagedoor = tempGarageDoor;
            }
    
            public string execute()
            {
                return this.garagedoor.Down();
            }
        }
    
        public class LightOnCommand : Command
        {
            Light light;
            public LightOnCommand(Light argLight)
            {
                this.light = argLight;
            }
    
            public string execute()
            {
                return this.light.On();
            }
        }
    
        public class SimpleRemoteControl
        {
            Command slot;
    
            public void SetCommand(Command command)
            {
                this.slot = command;
            }
    
            public string ButtonPressed()
            {
                return this.slot.execute();
            }
        }
    
    //************Client end************************************//
    SimpleRemoteControl remotecontrol = new SimpleRemoteControl();
    GarageDoor garagedoor = new GarageDoor();
    
    //encapsulates a request as an object
    GarageDoorOpenCommand objGarageDoorOpenCommand = new GarageDoorOpenCommand(garagedoor);
    //Loaded the button slot with a "Garage Door Open Command"
    remotecontrol.SetCommand(objGarageDoorOpenCommand);
    //From the outside,no other objects really know what actions get performed on what recevier
    Console.Write(remotecontrol.ButtonPressed() + "\n");
    
                
    //encapsulates a request as an object
    GarageDoorClosedCommand objGarageDoorClosedCommand = new GarageDoorClosedCommand(garagedoor);
    //Loaded the button slot with a "Garage Door Closed Command"
    remotecontrol.SetCommand(objGarageDoorClosedCommand);
    //From the outside,no other objects really know what actions get performed on what recevier
    Console.Write(remotecontrol.ButtonPressed() + "\n");
    
                
    //encapsulates a request as an object
    LightOnCommand objLight = new LightOnCommand(new Light());
    //Loaded the button slot with a "Light On"
    remotecontrol.SetCommand(objLight);
    //From the outside,no other objects really know what actions get performed on what recevier
    Console.Write(remotecontrol.ButtonPressed() + "\n");
                           
    //Remote slot didn't care what command object it had, as long as it implemented the command interface
    Console.ReadKey();