Connection to websocket fails?

I am new to web sockets and trying to create it using asp.net Generic Handler and JavaScript WebSocket Class

Javascript

<script type="text/javascript">
    window.onload= function () {

        var name = prompt('what is your name?:');            
        var url = 'ws://localhost:5707/ws.ashx?name=' + name;    
        var ws = new WebSocket(url);    
        ws.onopen = function () {
            alert('Connection Opened');
        };
            ws.onmessage = function (e) {
        };
            ws.onclose = function () {
            alert('Connection Close');
        };
            ws.onerror = function (e) {
            alert('Error')
        };

        }

</script>

C # Common handler called ws.ashx

public class ws : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.IsWebSocketRequest)
            context.AcceptWebSocketRequest(new TestWebSocketHandler());
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

The TestWebSocketHandler class, which inherits from WebSocketHandler

   public class TestWebSocketHandler : WebSocketHandler
    {
        private static WebSocketCollection clients = new WebSocketCollection();
        private string name;

        public override void OnOpen()
        {
            this.name = this.WebSocketContext.QueryString["name"];
            clients.Add(this);
            clients.Broadcast(name + " has connected.");
        }

        public override void OnMessage(string message)
        {
            clients.Broadcast(string.Format("{0} said: {1}", name, message));
        }

        public override void OnClose()
        {
            clients.Remove(this);
            clients.Broadcast(string.Format("{0} has gone away.", name));
        }

    }

My problem

when websocket is about to open, I noticed that when I went to the handler,

context.IsWebSocketRequest // returns false

and then it causes an error on the client, says

Firefox cannot establish a connection to the server at ws: // localhost: 5707 / ws.ashx? name = j

and then close the connection instance

I need to know where the problem is? kindly

I am using vs 2013 under Windows 7 and I think its IIS 6 which I am working on

+4
source share
2

WebSockets ASP.NET, Windows 8 Windows 2012, . , API .NET 4.5.1, , . , ClientWebSocket, PlatformNotSupportedException.

- , :

+5

:

  • IIS < 8
  • - Windows
  • .NET Framework < 4.5
  • web.config system.webServer :
<handlers>
    <add path="/ws.ashx" verb="*" name="ws" type="namespace.ws"/>
</handlers>
+1

Source: https://habr.com/ru/post/1534211/


All Articles