3

我从头开始用java开发了一个websocket服务器。javascript 客户端在浏览器中运行:

function connectToServer() {
    connection = new WebSocket("ws://" + document.domain + ":8351");       
    connection.onopen = function () {
    };

    connection.onmessage = function (e) {
        handleServerResponse(e.data);
    };
}

一切都很好,直到消息(json)达到 65535 字节。然后关闭套接字(我还没有弄清楚,如果客户端或服务器关闭连接。

在浏览器控制台(尝试了几个浏览器)中,我看到:与 ws://localhost:8351/ 的连接在页面加载时被中断。

在服务器端,我看到: java.net.SocketException: Connection reset at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)

因此,如果问题出在握手中,或者我在服务器中错误地构建了框架并且客户端关闭了连接,或者我正在写入不正确的字节流并且 java io 关闭了套接字。

我的代码(服务器):

1)握手(服务器响应)

  String _01 = "HTTP/1.1 101 Switching Protocols\r\n";
  String _02 = "Upgrade: websocket\r\n";
  String _03 = "Connection: Upgrade\r\n";
  String _04 = "Sec-WebSocket-Accept: " + responseKey + "\r\n";
  String _05 = "Content-Encoding: identity\r\n";

2)构建框架(是否应该将大消息分解为不同的框架?)

public static byte[] buildFrame(String message)
{
    int length = message.length();
    int rawDataIndex = -1;
    if (length <= 125)
        rawDataIndex = 2;
    else if (length >= 126 && length <= 65535)
        rawDataIndex = 4;
    else
        rawDataIndex = 10;
    byte[] frame = new byte[length + rawDataIndex];
    frame[0] = (byte)129;
    if (rawDataIndex == 2)
        frame[1] = (byte)length;
    else if (rawDataIndex == 4)
    {
        frame[1] = (byte)126;
        frame[2] = (byte)(( length >> 8 ) & (byte)255);
        frame[3] = (byte)(( length      ) & (byte)255);
    }
    else
    {
        frame[1] = (byte)127;
        frame[2] = (byte)(( length >> 56 ) & (byte)255);
        frame[3] = (byte)(( length >> 48 ) & (byte)255);
        frame[4] = (byte)(( length >> 40 ) & (byte)255);
        frame[5] = (byte)(( length >> 32 ) & (byte)255);
        frame[6] = (byte)(( length >> 24 ) & (byte)255);
        frame[7] = (byte)(( length >> 16 ) & (byte)255);
        frame[8] = (byte)(( length >>  8 ) & (byte)255);
        frame[9] = (byte)(( length       ) & (byte)255);

    }
    for (int i = 0; i < length; i++)
        frame[rawDataIndex + i] = (byte)message.charAt(i);
    return frame;
}

3)向套接字写入字节(我试过socket.setSendBufferSize和BufferedOutputStream,没有任何帮助)

socket.getOutputStream().write(byteMessage);
socket.getOutputStream().flush();

有没有人遇到过同样的问题?欢迎任何帮助!

4

2 回答 2

4

I know it's a bit late for answering... but anyway, i faced the same problem and this code:

    frame[1] = (byte)127;
    frame[2] = (byte)(( length >> 56 ) & (byte)255);
    frame[3] = (byte)(( length >> 48 ) & (byte)255);
    frame[4] = (byte)(( length >> 40 ) & (byte)255);
    frame[5] = (byte)(( length >> 32 ) & (byte)255);
    frame[6] = (byte)(( length >> 24 ) & (byte)255);
    frame[7] = (byte)(( length >> 16 ) & (byte)255);
    frame[8] = (byte)(( length >>  8 ) & (byte)255);
    frame[9] = (byte)(( length       ) & (byte)255);

works. Your problem here is that your variable "length" is an int that is 32bits. And you are performing bit shifting on 8 bytes so you get random data from the memory. If you define your variable "length" as a 64 bits long then it works.

long length = (long)message.length();

worked perfectly for me.

I hope it can help someone who wants to understand.

于 2014-07-30T17:40:28.563 回答
2

正如我所怀疑的那样,通过互联网填充的位移代码不起作用。

找到以下代码(来源):

 frame[1] = (byte)127;

            int left = length;
            int unit = 256;

            for (int i = 9; i > 1; i--)
            {
                frame[i] = (byte)(left % unit);
                left = left / unit;

                if (left == 0)
                    break;
            }

它有效!虽然我想不通,但有什么区别。

于 2013-07-17T15:41:27.183 回答