3

Hi I have Implemented the WebSocket Frame reading from this thread How can I send and receive WebSocket messages on the server side?, really thanks.

I successful implemented the handshaking. I succeeded on receiving message and reading the frame from Browser to Java.

Here is my implementation in Java on sending.

public void brodcast(String mess) throws IOException{
    byte[] rawData = mess.getBytes();

    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++;
    }

    out.write(reply);
    out.flush();

}

When I call this function like broadcast("Hello world"); the server broadcasts the message immediately and the browser receives the info and displays. Immediately the socket closes throwing "software caused connection abort socket write error". I am unable send th messages afterwords.

Community
  • 1
  • 1
  • Hi, I figured it out myself! it happens only when I connect using Mozilla Firefox. The Firefox sends two WebSocket requests each time I connect. I don't why! _may be a reference hook for Firebug._ The second unwanted connection from Mozilla caused the error I mentioned. **So what I did is** I created a separate Thread and let `out.write` on that, so the error is still there but its not affecting other reading or writing methods. – Haribabu Pasupathy Sep 20 '12 at 11:48

0 Answers0