javascript - Socket getting text string as hex array, multiple socket call in each other's event -


client:

var iosocket = io.connect(); iosocket.on('connect', function () {     iosocket.on('message', function(message) {     console.log(message)     }); });  $('#input').keypress(function(event) {     if(event.which == 13) {         event.preventdefault();         iosocket.send($('#input').val());     } }); 

server: (ingnore require part)

var socket = require('socket.io'); conn = function (socket) {     console.log("connnect");     socket.on('disconnect', function (socket) {     console.log("disconnect");     });    socket.on('message', function (data) {     var socket1 = new net.socket();     socket1.connect (port, host, function() {     socket1.write(data);     socket1.end();     });      socket1.on('data', function(data) {     socket.broadcast.emit('message', data);     socket.emit('message',data);     });      socket1.on('error', function(exception){     console.log('exception:');     console.log(exception);     });      socket1.on('drain', function() {     console.log("socket1 drain!");     });      socket1.on('timeout', function() {     console.log("socket1 timeout!");     });      socket1.on('close', function() {     console.log('socket1 closed');     }); }); } var io = socket.listen(server, { log: false }); io.sockets.on('connection', conn ); 

problem1(solved needs feedback): response (mkessage variable) getting in client in hex array format, tried setencoding , tostring method did not solve problem. following code converted hex array in readable string.

byte = ''; (var i=0; < data.length; i++) { byte += string.fromcharcode( parseint(data[i], 16).tostring(16) ); } 

problem 2 : tcp socket socket1 created every time , taking huge time this. how create , use socket1 don't closed after every write?
status of other guy listening @ port host force close?

have tried explicitly set encoding of socket?

socket.on('message', function (msg) {     var socket1 = new net.socket();     socket1.setencoding('utf8'); //< explicitly request utf8     socket1.connect (port, host, function() {         socket1.write(msg);         socket1.end();     }); 

see nodejs documentation more details.

if not help, share code of tcp server listening @ host:port (see socket1 initialization)?

edit

as mentioned in comment(s) below: unless call setencoding() on socket, 'data' callback receives buffer object. should convert string broadcast.

socket1.on('data', function(data) {     socket.broadcast.emit('message', data.tostring()); }); 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -