'Could not decode a text frame as UTF-8.

Now i have struggled to get this bug fixed, but i cannot find out how to fix it, i have searched and found a solution, but it wasn't exactly a solution for the problem.

It's everytime i "entering a room" in my chat-application i am getting disconnected with "Could not decode a text frame as UTF-8.".

  • I am sending all commands to WebSocket server (in PHP as json)

The command i send though WebSocket (ws://....) is:

  {"command": "roomchange", "data" : "14"}

My php websocket server handle the requist and send back thid which cause the problem:

{"command" : "NEWUSER","data" : { "mood" : "tired", "apartment" : "0","username" : "tester","seat_id" : "7","room_id" : "14","gender" : "male", "face" : "", "hair" : "http://xxx.dk/framework/gfxs/avatars/male/hair/d0fc74a351b05cae75258e93422c040e.png", "shoes" : "http://xxx.dk/framework/gfxs/avatars/male/shoes/00708715be7e7c007fca457eb5573637.png", "clothes" : "http://xxx.dk/framework/gfxs/avatars/male/shirts/9401f96ed9d6ad43c5d5ad7352e50de2.png"}}

and maybe this:

  {"command": "roombg","data" : { "id" : "14", "background" : "new.png", "name" : "Diskotek"}}

When i send request to my websocket server in php i have this to

return unescape( encodeURIComponent( JSON.stringify( COMMAND ) ) )

Is it cause the websockets send back the URL's? Or what is the problem?



Solution 1:[1]

That sounds like a bug in the PHP WebSocket server that you are using. It indicates that server has not properly constructed the WebSocket frame properly and it contains non-UTF8 data or the server split a message into two frames and the UTF-8 data got split in the middle of a multi-byte character encoding.

Also, you shouldn't need to escape(encodeURIComponent(STRING)). The result of JSON.stringify should already be safe to send as a WebSocket message.

Solution 2:[2]

It's a old subject, but in case people do search for similar bug I put an answer here.

Recently I encoutered problem with a php websocket server, the javascript client in chrome close the connection saying "could not decode a text frame as UTF8".

I searched the net in order to found a answer, and trying different response saying about replacing bad utf8 character, or using encodeURI or using unescape etc...

But after reading a part of the websocket specs, I checked the frame send on the php server. I see that for frame length under 126 bytes long it was working great, but for frame longer than 126 bytes, it's generate an error on the client side. After a little frame inspection I see that the length of the frame was stored in little endian order, or based on an example in the specs the frame length need to be in big endian order.

So after putting the right endian order for the length everything goes fine.

Solution 3:[3]

it is necessary to properly wrap the response from the server in a mask:

socket_write($socket, encode($answer));
function encode($text) {
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);
    if ($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif($length > 125 && $length < 65536)
        $header = pack('CCn', $b1, 126, $length);
    elseif($length >= 65536)
        $header = pack('CCNN', $b1, 127, $length);
    
    return $header.$text;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kanaka
Solution 2 BlackStrawHat
Solution 3 DividerBeam