客户端在服务器websocket的10bytes数据后断开连接

时间:2013-01-27 14:22:53

标签: websocket

我设法通过客户端服务器websocket连接的握手阶段,但现在我在将数据从服务器发送到客户端时遇到问题。以下是发送数据的代码:

public void Send(String s)throws IOException{

os = socket.getOutputStream();

byte[] rawData = s.getBytes("UTF-8");

int frameCount  = 0;
byte[] frame = new byte[10];

frame[0] = (byte) 129;

if(rawData.length <= 125){
    frame[1] = (byte) rawData.length;
    frameCount = 2;
}else if(rawData.length >= 126 && rawData.length <= 65535){
    frame[1] = (byte) 126;
    byte len = (byte) rawData.length;
    frame[2] = (byte)((len >> 8 ) & (byte)255);
    frame[3] = (byte)(len & (byte)255); 
    frameCount = 4;
}else{
    frame[1] = (byte) 127;
    byte len = (byte) rawData.length;
    frame[2] = (byte)((len >> 56 ) & (byte)255);
    frame[3] = (byte)((len >> 48 ) & (byte)255);
    frame[4] = (byte)((len >> 40 ) & (byte)255);
    frame[5] = (byte)((len >> 32 ) & (byte)255);
    frame[6] = (byte)((len >> 24 ) & (byte)255);
    frame[7] = (byte)((len >> 16 ) & (byte)255);
    frame[8] = (byte)((len >> 8 ) & (byte)255);
    frame[9] = (byte)(len & (byte)255);
    frameCount = 10;
}

int bLength = frameCount + rawData.length;

byte[] reply = new byte[bLength];

int bLim = 0;
for(int i=0; i<frameCount;i++){
    reply[bLim] = frame[i];
    bLim++;
}
for(int i=0; i<rawData.length;i++){
    reply[bLim] = rawData[i];
    bLim++;
}
System.out.println(frame);
os.write(reply);
os.flush();
}

应该将字符串s发送给客户端。 我可以发送9bytes的数据,但是echo websocket网站上的测试客户端没有收到它,如果我发送第十个字节,客户端断开连接说“error:unknown”。 知道我做错了什么吗? 提前致谢! 斯摩列特。

0 个答案:

没有答案
相关问题