简体   繁体   English

使用 Javascript 和 C# 连接到套接字时出现错误“WebSocket 打开握手超时”

[英]Connection to socket gets error “WebSocket opening handshake timed out” using Javascript and C#

About 4 hours of research...here we go.大约 4 小时的研究......这里我们 go。

I have a C# program that sends and listens for anything coming in a specific Socket.我有一个 C# 程序,它发送和侦听特定套接字中的任何内容。 Using the sockets, C# can send stuff to it and can receive from it just fine.使用 sockets,C# 可以向它发送东西,也可以从它那里接收。 Now, going to my JavaScript file, I'm using the WebSocket interface to communicate with C#, but doesn't work (usually times out after a couple of minutes).现在,转到我的 JavaScript 文件,我正在使用 WebSocket 接口与 C# 通信,但不起作用(通常在几分钟后超时)。 When the Socket is online, the JavaScript code will take up to about 4 minutes then throw an error saying "WebSocket opening handshake timed out".当 Socket 在线时,JavaScript 代码将需要大约 4 分钟,然后抛出一个错误,提示“WebSocket 打开握手超时”。 The thing is I know that it can find because, when the port of the ip doesn't exist the JavaScript file throws an error in the next couple seconds.问题是我知道它可以找到,因为当 ip 的端口不存在时,JavaScript 文件会在接下来的几秒钟内引发错误。

Things I've done:我做过的事情:

Turn off all firewalls, use both ws and wss at the beginning of the ip and port (ex: wss://xxx.xxx.x.xx:11111), change the port, change the ip to a valid ip still reachable, research for 4 hours. Turn off all firewalls, use both ws and wss at the beginning of the ip and port (ex: wss://xxx.xxx.x.xx:11111), change the port, change the ip to a valid ip still reachable,研究4小时。

C#: C#:

IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = IPAddress.Parse("ip");
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);

Socket listener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

try
{
    listener.Bind(localEndPoint);

    listener.Listen(10);

    while (true)
    {

        Console.WriteLine("Waiting connection...");

        Socket clientSocket = listener.Accept();

        byte[] bytes = new Byte[1024];
        string data = null;

        while (true)
        {

            int numByte = clientSocket.Receive(bytes);

            data += Encoding.ASCII.GetString(bytes, 0, numByte);

            if (data.IndexOf("<EOF>") > -1)
            {
                break;
            }
        }

        Console.WriteLine("Text received -> {0} ", data);
        byte[] message = Encoding.ASCII.GetBytes("Test Server");

        clientSocket.Send(message);

        clientSocket.Shutdown(SocketShutdown.Both);
        clientSocket.Close();
    }
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

JavaScript: JavaScript:

socket = new WebSocket("wss://ip:11111");
socket.onopen = function()
{
    alert("Connected!");
}
socket.onerror = function()
{
    alert("Connection Failed");
}

The ip is local ip 是本地的

Long story short, C# can communicate with itself and JavaScript can find it but can't communicate with it.长话短说,C# 可以与自己通信,而 JavaScript 可以找到它但无法与其通信。

Properly complete a handshake.正确完成握手。 (Or use a library / connection type that does.) (或者使用一个库/连接类型。)

The WebSocket protocol (as original defined in RFC6455 - The WebSocket Protocol ) does not open a plain unrestricted socket, in part for security reasons. WebSocket 协议(如 RFC6455 中的原始定义- WebSocket 协议不会打开普通的不受限制的套接字,部分原因是出于安全原因。

Since the handshake is not complete, the client WS request will timeout as the HTTP “Upgrade” response is never received.由于握手未完成,客户端 WS 请求将超时,因为从未收到 HTTP“升级”响应。 Until the handshake is complete, the WS will not be active over the underlying TCP connection.在握手完成之前,WS 不会在底层 TCP 连接上处于活动状态。

Initiating a WebSocket connection (“the handshake”) is defined in section 4 of the RFC.启动 WebSocket 连接(“握手”)在 RFC 的第 4 节中定义。 It is also discussed in How JavaScript works: Deep dive into WebSockets and HTTP/2 with SSE + how to pick the right path . JavaScript 的工作原理中也对此进行了讨论:使用 SSE 深入研究 WebSockets 和 HTTP/2 + 如何选择正确的路径

The client establishes a WebSocket connection through a process known as the WebSocket handshake.客户端通过称为 WebSocket 握手的过程建立 WebSocket 连接。 This process starts with the client sending a regular HTTP request to the server.此过程从客户端向服务器发送常规 HTTP 请求开始。 An Upgrade header is included in this request which informs the server that the client wishes to establish a WebSocket connection.升级 header 包含在此请求中,通知服务器客户端希望建立 WebSocket 连接。

.. ..

Now that [after] the handshake is complete the initial HTTP connection is replaced by a WebSocket connection that uses the same underlying TCP/IP connection.现在[之后] 握手完成,初始 HTTP 连接被使用相同底层 TCP/IP 连接的 WebSocket 连接替换。 At this point, either party can start sending data.此时,任何一方都可以开始发送数据。

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

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