0

I am making a chat application and I can manage to send messages in Unicode from the clients, but if I write an instruction in the server code (nodejs script with net framework), for example attaching the nick of the sender to the message, it makes the message garbled.

Example: sockets[i].write(socket.id + ": " + msg_sent);

If msg_sent is in ASCII (encoded as ascii in the client), it works fine, but if I encode it as Unicode, the instruction makes the message unreadable.

Thanks in advance for the help.

1 Answers1

0

If you need UTF-16, node supports the 'utf16le' encoding which is UTF-16 in little-endian format. To use that, simply specify the encoding:

sockets[i].write(socket.id + ": " + msg_sent, 'utf16le');

If you need some other encoding that node does not natively support, you will need to use something like iconv.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • IS 'utf16be' supported? Is there any documentation about this topic? Thanks. – user3603880 May 26 '14 at 11:14
  • 'utf16be' is not supported in node core. You may have to use something like [iconv](https://github.com/bnoordhuis/node-iconv) if you need anything beyond the [built-in encodings](http://nodejs.org/docs/latest/api/buffer.html#buffer_buffer). – mscdex May 26 '14 at 13:57