WebSocket server refusing connections.

Discussion in 'Windows 2012 R2/.NET 4.5.1 Beta [Closed]' started by fmaeseele, Aug 25, 2013.

  1. Hi,
    Thanks to this lab, I wanted to test a simple websocket chat server.
    Everything works fine on IIS Express and IIS on my local machine.
    But once the site published, I can't connect to endpoint through any way (Javascript websocket or native WebSocket c#).
    Is there any restrictions on this ?

    Here is my Handler:

    Code:
        public class WSHandler : IHttpHandler
        {
     
            public void ProcessRequest(HttpContext context)
            {
                if (context.IsWebSocketRequest)
                {
                    try
                    {
                        context.AcceptWebSocketRequest(ProcessWSChat);
                    }
                    catch(Exception e)
                    {
                        context.Response.StatusCode = 400;
                    }
                }
                else
                    context.Response.StatusCode = 400;
            }
     
            public bool IsReusable { get { return false; } }
     
            List<WebSocket> socketList = new List<WebSocket>();
     
            private async Task ProcessWSChat(AspNetWebSocketContext context)
            {
                WebSocket socket = context.WebSocket;
                socketList.Add(socket);
                try
                {
                    while (true)
                    {
                        ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
                        WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);
                        if (socket.State == WebSocketState.Open)
                        {
                            if (result.MessageType == WebSocketMessageType.Text && result.EndOfMessage)
                            {
     
                                string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
                                buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage));
                                Parallel.ForEach(socketList, (s) => s.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None));
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch(Exception ex)
                {
     
                }
            }
        }
    
    Thanks,

    François
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    If you haven't done so already, you need to open up a support ticket to have WebSockets enabled for your account. Your account also needs to be on Windows 2012/IIS8 as it is not supported on the other platforms we offer.
     
  3. Hi,
    Thanks for your reply.
    Regarding the requirements, this is ok as I'm beta testing Windows 2012/IIS8 environment.
    Regarding the support, I need an account and I have only the labs account :-(

    François
     

Share This Page