node server can't show characters besides English

235 views
Skip to first unread message

yougen zhuang

unread,
Oct 23, 2013, 11:35:51 PM10/23/13
to nod...@googlegroups.com
Even setting ReadableStream.setEncoding('utf8'), node still can't show Chinese or other chareacters.

===========server.js===============
var net = require('net');

var server = net.createServer();
var clients = [];

server.on('connection', function(socket){
   console.log("got a new connection");
   socket.id = parseInt(Math.random()*1000, 10);
   clients.push(socket);
   socket.on('data', function(data){
       console.log('got data:', data.toString());
       socket.setEncoding('utf8');

       clients.forEach(function(otherSocket){
          if(otherSocket !== socket) {
              otherSocket.write(data);
          }
       });
   })

});

server.on('error', function(err){
   console.log('Server error: ', err.message);
});

server.on('close', function(){
    console.log('Server closed');
    var index = clients.indexOf(socket);
    clients.splice(index, 1);
});

server.listen(4001);

=============client.js=================
var net = require('net');
var port = 4001;
var client;

process.stdin.resume();

(function connect(){
   client = net.createConnection(port);
   client.setEncoding('utf8');

   client.on('connect', function(){
       console.log("connected to server");
   });

   client.on('error', function(err){
       console.log('Error in connection:', err);
   });

   client.on('close', function(){
       console.log('connection got closed, will try to reconnect');
   });

   client.pipe(process.stdout, {end:false});

   process.stdin.pipe(client);
}());




mscdex

unread,
Oct 24, 2013, 12:57:38 AM10/24/13
to nod...@googlegroups.com
On Wednesday, October 23, 2013 11:35:51 PM UTC-4, yougen zhuang wrote:
Even setting ReadableStream.setEncoding('utf8'), node still can't show Chinese or other chareacters.


Are you sure the text is (valid) utf-8 and not some other character set?

bluezyg

unread,
Oct 24, 2013, 10:02:44 PM10/24/13
to nod...@googlegroups.com
I write some Chinese character into the stdin, isn't it valid utf8?


2013/10/24 mscdex <msc...@gmail.com>
On Wednesday, October 23, 2013 11:35:51 PM UTC-4, yougen zhuang wrote:
Even setting ReadableStream.setEncoding('utf8'), node still can't show Chinese or other chareacters.


Are you sure the text is (valid) utf-8 and not some other character set?

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/N_681AM3bXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

greelgorke

unread,
Oct 25, 2013, 5:08:21 AM10/25/13
to nod...@googlegroups.com
it depends on how your terminal is configured. on mac osX for example you can choose wich character encoding should be used.

Tim Caswell

unread,
Oct 25, 2013, 10:49:59 AM10/25/13
to nod...@googlegroups.com
Also keep in mind that the native string type in JavaScript is UCS-16 which means that any code points higher than 16 bits have to be encoded using surrogate pairs. (Note that the native encoding is quite different from the UTF-8 encoding commonly used when serializing strings to binary data)

A quick way to check your terminal's settings is to read stdin as node buffers and inspect the bytes manually.  UTF-8 isn't too hard to recognize and decode by hand for just a few chars.


You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

bluezyg

unread,
Oct 27, 2013, 4:26:58 AM10/27/13
to nod...@googlegroups.com
"read stdin as node buffers and inspect the bytes manually", what does it mean? Could you show code snippet? Thx


2013/10/25 Tim Caswell <t...@creationix.com>
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages