简体   繁体   English

Java Websocket客户端未收到服务器的响应

[英]Java Websocket Client Not Receiving Response from Server

I've spent some time trawling through stack overflow and google, but alas I was unable to find the what I've been looking for (there may be something I've missed, sorry if there is). 我花了一些时间来浏览堆栈溢出和Google,但可惜我找不到我一直在寻找的东西(可能我错过了一些东西,抱歉。 This is driving me insane as I'm so certain it all looks right, but obviously something is wrong and I just can't see it. 我非常确定这一切看起来都对,这使我发疯,但是显然出了点问题,我只是看不到。

Basically, the handshake is fine. 基本上,握手很好。 I receive and parse packets from the client fine. 我很好地从客户端接收和解析数据包。

When I try to send framed packet data back to the client, the client does nothing (not even an error). 当我尝试将成帧的数据包数据发送回客户端时,客户端什么也不做(甚至没有错误)。 I've tried it on the latest Firefox and Chrome browsers using the example on echo.websocket.org and my own written javascript object. 我已经使用echo.websocket.org上的示例以及我自己编写的javascript对象在最新的Firefox和Chrome浏览器上进行了尝试。 I can see the packet being sent and received via Wireshark on both the client and the server. 我可以在客户端和服务器上看到通过Wireshark发送和接收的数据包。

Here is an example of what I'm sending and a breakdown of the packet according to the RFC 6455 standards. 这是我正在发送的示例以及根据RFC 6455标准进行的数据包分解。

        Entire Response Packet Sent : 
            00 16 d3 65 d0 50 f4 6d 
            04 00 5f 84 08 00 45 00 
            00 32 5f e3 40 00 80 06 
            17 8c c0 a8 01 02 c0 a8 
            01 04 1f 91 05 18 fd 56 
            6f 03 ea 17 6d 4c 50 18 
            01 00 c0 fa 00 00 81 84 
            b2 3f de 11 83 0d ed 25

        Payload Data Unmasked Should Read : 1234

        Data : 
            81 84 B2 3F DE 11 83 0D ED 25

            FIN, RSV 1-3 and Op Code :

            0x81    1000 0001
                FIN     1 
                RSV1    0
                RSV2    0
                RSV3    0
                Opcode  1

            Mask and Payload Length :

            0x84    1000 0100
                Mask    1
                Length  4

            Mask Key :

            0xB2    1011 0010
            0x3F    0011 1111
            0xDE    1101 1110
            0x11    0001 0001

            Masked Payload Data : 

            0x83    1000 0011
            0x0D    0000 1101
            0xED    1110 1101
            0x25    0010 0101

            Unmasked Data

            Binary          Decimal         Hex         UTF-8

            1011 0010  
            1000 0011 ^
            -----------
            0011 0001       49              31          1

            0011 1111 
            0000 1101 ^
            -----------
            0011 0010       50              32          2

            1101 1110
            1110 1101 ^
            -----------
            0011 0011       51              33          3

            0001 0001 
            0010 0101 ^
            -----------
            0011 0100       52              34          4

My entire codebase is on GitHub, but here are the core files : 我的整个代码库都在GitHub上,但这是核心文件:

Data Frame Structure : https://github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/framing/DataFrame.java 数据框架结构: https : //github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/framing/DataFrame.java

Data Frame Response Builder : https://github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/framing/DataFrameResponse.java 数据框响应生成器: https : //github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/framing/DataFrameResponse.java

Client Socket Connection Thread : https://github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/threads/SocketClient.java 客户端套接字连接线程: https : //github.com/willitscale/simplejavawebsocket/blob/master/java/src/uk/co/n3tw0rk/websocketregistration/threads/SocketClient.java

Whole repository : https://github.com/willitscale/simplejavawebsocket 整个仓库: https : //github.com/willitscale/simplejavawebsocket

Any help would be much appreciated! 任何帮助将非常感激!

A bit more detail on wiring up your WebSocket client library to the autobahn websocket protocol testsuite. 有关将WebSocket客户端库连接到高速公路Websocket协议测试套件的更多详细信息。

http://autobahn.ws/testsuite/manual/#fuzzing http://autobahn.ws/testsuite/manual/#fuzzing

  1. Create a spec for yourself fuzzingserver.json 为自己创建一个规范fuzzingserver.json
{
   "url": "ws://127.0.0.1:9001",

   "options": {"failByDrop": false},
   "outdir": "./reports/clients",
   "webport": 8080,

   "cases": ["*"],
   "exclude-cases": [],
   "exclude-agent-cases": {}
}

This file allows you to tweak what test cases should be run, what your port numbers are, etc . 该文件使您可以调整应运行哪些测试用例,端口号等。

  1. Start the autobahn fuzzing server 启动高速公路模糊测试服务器

    $ wstest -m fuzzingserver $ wstest -m fuzzingserver

  2. In your client code 在您的客户代码中

    A: Request the number of cases. 答:要求案件数量。

    ws://localhost:9001/getCaseCount

    You will receive a TEXT message, an Integer representing the number of test cases 您将收到一条TEXT消息,一个整数,代表测试用例的数量

    B: Connect and run specific test case (by number) B:连接并运行特定的测试用例(按编号)

    ws://localhost:9001/runCase?case={case_number}&agent={library-name}

    Be sure to echo back any complete TEXT or BINARY messages received to the server. 确保回显收到到服务器的所有完整的TEXT或BINARY消息。

    C: Generate the Reports C:生成报告

    ws://localhost:9001/updateReports?agent={library-name}

You should now have a report of the testcases you ran on disk, in the path you specified in the fuzzingserver.json above 现在,您应该在上面的fuzzingserver.json指定的路径中具有在磁盘上运行的测试用例的报告。

Finally got to the bottom of it! 终于深究了!

Although the handshake appeared to work it actually screwed up the buffer on the client. 尽管握手似乎可以正常工作,但实际上却弄糟了客户端上的缓冲区。

My response to the client was a half and half, half from the RFC6455 and half from HYBI standards where the handshake key was in the body of the response. 我对客户端的响应是一半,一半来自RFC6455,一半来自HYBI标准,其中握手密钥位于响应的主体中。

This is just a theory and I've not looked into it but I believe it stopped reading the buffer after the "\\r\\n\\r\\n", when I sent a message the first thing it must of read was the accept key left over from the buffer the first time. 这只是一个理论,我没有研究过,但是我相信它在“ \\ r \\ n \\ r \\ n”之后停止读取缓冲区,当我发送消息时,它必须读取的第一件事是accept键第一次离开缓冲区。

            "Sec-WebSocket-Accept: " + Utils.generateKey( wsrRequest.socketKey ) + "\r\n" +
            "\r\n" + Utils.generateKey( wsrRequest.socketKey );

Thanks for all your help, never fear it will not be in vein as you've opened my eyes to Autobahn Test Suite! 感谢您的所有帮助,不要担心,因为您对我的Autobahn Test Suite睁开了眼睛!

Curious though Autobahn didn't pick this issue with the handshake up, nor did chrome or firebug? 好奇的是,尽管高速公路没有通过握手解决这个问题,但是镀铬或萤火虫也没有? A bug perhaps? 可能是个错误?

Why is it always me who finds these kinds of things /sigh 为什么总是我发现这些东西/叹息

Last week it was an actual bug with a payment service after days of investigating and thinking I was wrong, it was the service that didn't do what the documentation said. 在经过数天的调查并认为我错了之后,上周这是付款服务的一个实际错误,这是该服务没有按照文档所述执行。 Luckily in this cause I was wrong! 幸运的是,由于这个原因,我错了!

Either way, onward and upward I can finally continue my development. 无论哪种方式,我都可以继续向前和向上发展。 After about a day and half set back while I was convinced that java was screwing up the bytes, tcp packets or Websockets would only work on a non-blocking full duplex socket connection and not a half duplex socket connection. 经过大约一天半的时间后,我确信java搞砸了字节,tcp数据包或Websockets仅适用于非阻塞的全双工套接字连接,而不适用于半双工套接字连接。

I'm my own worst enemy 我是我自己最大的敌人

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

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