none
Can´t connect a WCF Self-hosted WebSocket service and a Javascript client. RRS feed

  • Question

  • Hello,

    I´m developing a WCF WebSockets self-hosted server to share services with a Javascript client.

    I´ve tested the WCF server with a console client application and it works great, but i can´t figure out how to make it work with my Javascript code.

    I´ve followed this and this tutorials, but since their servers are implemented to work on an IIS server, they don´t match my needs and i get lost.

    This is how i´ve implemented the server:

    Self-hosted Console MAIN:

    //Create a URI to serve as the base address
                Uri httpUrl = new Uri("http://192.168.1.95:8080/service");
                //Create ServiceHost
                ServiceHost host = new ServiceHost(typeof(WebSocketService), httpUrl);            
                //Add a service endpoint
                host.AddServiceEndpoint(typeof(IWebSocket), new NetHttpBinding(), "");
                //Enable metadata exchange
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);
                //Start the Service
                host.Open();
    
                Console.WriteLine("Service is host at " + DateTime.Now.ToString());
                Console.WriteLine("Host is running... Press <Enter> key to stop");
                Console.ReadLine();

    SERVICE:

    namespace IWebSocketHostTest
    {
        [ServiceContract]
        interface IWebSocketCallBack
        {
            [OperationContract(IsOneWay = true)]
            void Send(int num);
        }
    
        [ServiceContract(CallbackContract = typeof(IWebSocketCallBack))]
        public interface IWebSocket
        {
            [OperationContract]
            void StartSend();
        }
    }

    namespace IWebSocketHostTest
    {
        class WebSocketService : IWebSocket
        {
            Timer timer = null;
    
            List<IWebSocketCallBack> callbackClientList = null;        
    
            public WebSocketService()
            {
                callbackClientList = new List<IWebSocketCallBack>();
                
                timer = new Timer(3000);
                timer.Elapsed += new ElapsedEventHandler(sendNumber);
                timer.Start();
            }
    
            private void sendNumber(Object o, ElapsedEventArgs eea)
            {
                timer.Stop();
                var random = new Random();
                int randomNum = random.Next(100);
                foreach (IWebSocketCallBack callback in callbackClientList)
                {
                    callback.Send(randomNum);
                }
                
                timer.Interval = random.Next(1000, 10000);
                timer.Start();
            }
            
            public void StartSend()
            {
                sender.addClient(OperationContext.Current.GetCallbackChannel<IWebSocketCallBack>());            
            }
    
        }
    }

    At the Javascript code, i´ve just tried to connect to the service doing this:

                var ws = new WebSocket("ws://192.168.1.95:8080/service");
                    ws.onopen = function () {
                        console.log("WEBSOCKET CONNECTED");
                    };
                    

    As you can see i´m realy lost with the client part. I´m just trying to connect to the service but it returns "WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request".

    So please, if you know any code example to connect a Javascript WebSocket object to a WCF self-hosted service i will apreciate it much.

    Thanks for your time.

    Friday, June 13, 2014 11:55 AM

Answers

All replies

  • Hello,

    I´m developing a WCF WebSockets self-hosted server to share services with a Javascript client.

    I´ve tested the WCF server with a console client application and it works great, but i can´t figure out how to make it work with my Javascript code.

    I´ve followed this and this tutorials, but since their servers are implemented to work on an IIS server, they don´t match my needs and i get lost.

    This is how i´ve implemented the server:

    Self-hosted Console MAIN:

    //Create a URI to serve as the base address
                Uri httpUrl = new Uri("http://192.168.1.95:8080/service");
                //Create ServiceHost
                ServiceHost host = new ServiceHost(typeof(WebSocketService), httpUrl);            
                //Add a service endpoint
                host.AddServiceEndpoint(typeof(IWebSocket), new NetHttpBinding(), "");
                //Enable metadata exchange
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);
                //Start the Service
                host.Open();
    
                Console.WriteLine("Service is host at " + DateTime.Now.ToString());
                Console.WriteLine("Host is running... Press <Enter> key to stop");
                Console.ReadLine();

    SERVICE:

    namespace IWebSocketHostTest
    {
        [ServiceContract]
        interface IWebSocketCallBack
        {
            [OperationContract(IsOneWay = true)]
            void Send(int num);
        }
    
        [ServiceContract(CallbackContract = typeof(IWebSocketCallBack))]
        public interface IWebSocket
        {
            [OperationContract]
            void StartSend();
        }
    }

    namespace IWebSocketHostTest
    {
        class WebSocketService : IWebSocket
        {
            Timer timer = null;
    
            List<IWebSocketCallBack> callbackClientList = null;        
    
            public WebSocketService()
            {
                callbackClientList = new List<IWebSocketCallBack>();
                
                timer = new Timer(3000);
                timer.Elapsed += new ElapsedEventHandler(sendNumber);
                timer.Start();
            }
    
            private void sendNumber(Object o, ElapsedEventArgs eea)
            {
                timer.Stop();
                var random = new Random();
                int randomNum = random.Next(100);
                foreach (IWebSocketCallBack callback in callbackClientList)
                {
                    callback.Send(randomNum);
                }
                
                timer.Interval = random.Next(1000, 10000);
                timer.Start();
            }
            
            public void StartSend()
            {
                sender.addClient(OperationContext.Current.GetCallbackChannel<IWebSocketCallBack>());            
            }
    
        }
    }

    At the Javascript code, i´ve just tried to connect to the service doing this:

                var ws = new WebSocket("ws://192.168.1.95:8080/service");
                    ws.onopen = function () {
                        console.log("WEBSOCKET CONNECTED");
                    };
                    

    As you can see i´m realy lost with the client part. I´m just trying to connect to the service but it returns "WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request".

    So please, if you know any code example to connect a Javascript WebSocket object to a WCF self-hosted service i will apreciate it much.

    Thanks for your time.

    Friday, June 13, 2014 11:55 AM
  • So please, if you know any code example to connect a Javascript WebSocket object to a WCF self-hosted service i will apreciate it much.

    Thanks for your time.

    Hi,

    Please try to check the following articles to see if you have done something wrong:
    #Building a JavaScript WebSockets Client for WCF:
    http://visualstudiomagazine.com/articles/2012/06/07/javascript-websockets-client.aspx .

    #Using WebSocket in WCF .NET 4.5:
    http://www.codeproject.com/Articles/620731/Using-WebSocket-in-NET-4-5-Part-4 .

    #WebSockets in ASP.NET and JavaScript:
    http://developers.de/blogs/damir_dobric/archive/2012/01/29/websockets-in-asp-net-and-javascript.aspx .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 16, 2014 9:38 AM
    Moderator