Feedjit

Articles for you

Saturday, June 22, 2013

Asp.NET Signalr SelfHost Server in Console. C# Asp.NET Signalr with WPF Client/Console Client/ JavaScript Client

using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.AspNet.SignalR.Hubs;
using System.Threading.Tasks;
using System.Threading;

namespace SignalR.Hosting.Self.Samples
{
    class Program
    {
     public  IHubContext context = GlobalHost.ConnectionManager.GetHubContext<CommunicationHub>();
        static public void Tick(Object stateInfo)
        {
           // context.Clients.All.joined("HELLO");
            //Console.WriteLine(context.Clients.ToString());
            Console.WriteLine("Tick: {0}", DateTime.Now.ToString("h:mm:ss"));
           
            //context.Clients.All.joined("Tick: {0}", DateTime.Now.ToString("h:mm:ss"));
        }
       // DateTime obje = new DateTime();
       
        public static string message=null;

       // public void Messagetoall()
       //{
       //     context.Clients.All.joined("HeLLO"); }
        static void Main(string[] args)
        {
            TimerCallback callback = new TimerCallback(Tick);
           
            string url = "http://localhost:8081";

            using (WebApplication.Start<Startup>(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.WriteLine("Creating timer: {0}\n",
                             DateTime.Now.ToString("h:mm:ss"));
                for (int i = 0; i < 100; i++)
                { Thread.Sleep(50); }
                    //while (true)
                    //{// broadcast obj = new broadcast();
                    //    // obj.Messagetoall();
                    //   // Program prg = new Program();
                    //   // prg.Messagetoall();
                    //}
                // create a one second timer tick
                Timer stateTimer = new Timer(callback, null, 0, 1000);
                // loop here forever
                for (; ; )
                {            }
            }
           
        }
    }
    //public class broadcast:Program
    //{

    //    public void Messagetoall()
    //    { context.Clients.All("HeLLO"); }
    //}

 public   class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Turn cross domain on
            var config = new HubConfiguration { EnableCrossDomain = true };

            // This will map out to http://localhost:8080/signalr by default
            app.MapHubs(config);
        }
    }
    [HubName("commHub")]
    public class CommunicationHub : Hub
    {
        public void send(string message)
        {
            Console.WriteLine(message);
            Clients.Caller.broadCastToClients(message);
        }
        public override Task OnConnected()
        {
            Console.WriteLine("Request from "+Context.ConnectionId+" is recieved");
            return Clients.All.joined(Context.ConnectionId, DateTime.Now.ToString());
        }
        public override Task OnDisconnected()
        {
            Console.WriteLine(Context.ConnectionId + " is Disconnected");
            return Clients.All.left(Context.ConnectionId, DateTime.Now.ToString());
        }
    }
}

Read More

Articles for you