无法将文本框架解码为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?

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.