简体   繁体   English

websocket连接失败?

[英]websocket connection fails?

I'm new in web sockets and trying to create one using asp.net Generic Handler and JavaScript WebSocket Class 我是Web套接字的新手,并尝试使用asp.net Generic Handler和JavaScript WebSocket Class创建一个

JavaScript 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# Generic Handler Called ws.ashx C#Generic Handler称为ws.ashx

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

Class TestWebSocketHandler which is inherits from WebSocketHandler 类TestWebSocketHandler,它继承自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 is 我的问题是

when the websocket intend to open i noticed that when went to the handler the 当websocket打算打开时,我注意到当去了处理程序时

context.IsWebSocketRequest // returns false

and then it fires Error on the client says 然后它会激活客户端的错误说

Firefox can't establish a connection to the server at ws://localhost:5707/ws.ashx?name=j Firefox无法在ws:// localhost:5707 / ws.ashx?name = j建立与服务器的连接

and then close the connection instantiation 然后关闭连接实例化

i need to know where is the problem ? 我需要知道问题出在哪里? kindly 和蔼

i'm using vs 2013 under windows 7 and i think its IIS 6 which i work on 我在Windows 7下使用vs 2013,我认为它是我工作的IIS 6

WebSockets will only work on ASP.NET applications running on Windows 8 or Windows 2012 I am afraid. WebSockets仅适用于在Windows 8或Windows 2012上运行的ASP.NET应用程序。 Despite of the API been included on .NET 4.5.1, it won't work if you are not using those operating system. 尽管API已包含在.NET 4.5.1中,但如果您不使用这些操作系统,它将无法运行。 Actually, if you try to use the ClientWebSocket it will throw a PlatformNotSupportedException . 实际上,如果您尝试使用ClientWebSocket ,它将抛出PlatformNotSupportedException

If you cannot get any of those operating systems, you can check alternatives: 如果您无法获得任何这些操作系统,可以查看备选方案:

Reason could be any of these: 原因可能是以下任何一种:

  1. Having IIS version < 8 IIS版本<8
  2. Not enabled Web Socket Protocol under windows features Windows功能下未启用Web套接字协议
  3. Having .NET Framework version < 4.5 .NET Framework版本<4.5
  4. Not included the following lines in web.config's system.webServer section: 不包括web.config的system.webServer部分中的以下行:
<handlers>
    <add path="/ws.ashx" verb="*" name="ws" type="namespace.ws"/>
</handlers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM