0

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 but needs feedback): The response (mkessage variable) I was getting in client was in hex array format, I tried setencoding and tostring method but it did not solve the problem. The following code converted the hex array in readable string.

byte = '';
for (var i=0; i < data.length; i++) {
byte += String.fromCharCode( parseInt(data[i], 16).toString(16) );
}

Problem 2 : The tcp socket socket1 is created for every time and it is taking huge time to do this. how do I create and use socket1 so that it don't get closed after every write?
Does the status of other guy listening at PORT HOST force it to close?

2
  • How about Code Snippets?
    – Didatus
    Commented Mar 4, 2013 at 12:26
  • I have added the code. its not that useful for solving my problem
    – Gaurav
    Commented Mar 4, 2013 at 12:43

1 Answer 1

2

Have you tried to explicitly set encoding of your 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 for more details.

If this does not help, could you share the code of TCP server listening at HOST:PORT (see socket1 initialization)?

Edit

As mentioned in the comment(s) below: unless you call setEncoding() on your socket, 'data' callback receives Buffer object. You should convert it to String for the broadcast.

socket1.on('data', function(data) {
    socket.broadcast.emit('message', data.toString());
});
2
  • I tried set encoding for socket1.. may be I did something wrong var server = http.createServer(function(req, res) { res.writeHead(200, { 'Content-type': 'text/html'}); res.end(fs.readFileSync(__dirname + '/index.html')); }); server.listen(8080, function() { console.log('Listening at: localhost:8080'); }); socketio.listen(server, { log: false }).on('connection', function (socket) {
    – Gaurav
    Commented Mar 4, 2013 at 13:28
  • 2
    The thing is, callback for socket1.on('data') will receive buffer object (not a string). You should either use data.toString() for emitting socket.io event -or- explicitly set socket1 encoding. Commented Mar 4, 2013 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.