Keith
Keith

Reputation: 2628

NodeJS help proxy TCP port data to web socket

I'm trying to proxy json data from a private TCP port 13854 to a public web socket on port 8080. Why can't I get any data when browsing http://localhost:8080?

var http = require('http').createServer(httpHandler),
    fs = require("fs"),
    wsock = require('socket.io').listen(http),
    tcpsock = require('net');

var proxyPort = 8080;
var serviceHost = 'localhost';
var servicePort = 13854;

function httpHandler (req, res) {
  res.setHeader("Access-Control-Allow-Origin", "http://example.com");
  res.end();
}

http.listen(proxyPort);
console.info("HTTP server listening on " + proxyPort);

wsock.sockets.on('connection', function (socket) { 

    var tcpClient = new tcpsock.Socket();
    tcpClient.setEncoding("ascii");
    tcpClient.setKeepAlive(true);

    tcpClient.connect(servicePort, serviceHost, function() {
        console.info('CONNECTED TO : ' + serviceHost + ':' + servicePort);

        tcpClient.on('data', function(data) {
        data = "" + data
        //send format request to socket
        if (data[0] != '{'){
            s.write(JSON.stringify({
                enableRawOutput : false,
                format : "Json"
            }) + "\n");
            return;
        }
            console.log('DATA: ' + data);
            socket.emit("httpServer", data);
        });

        tcpClient.on('end', function(data) {
            console.log('END DATA : ' + data);
        });
    });

    socket.on('tcp-manager', function(message) {
        console.log('"tcp" : ' + message);
        return;
    });

    socket.emit("httpServer", "Initial Data");
});

THANKS!

Upvotes: 1

Views: 1802

Answers (2)

Keith
Keith

Reputation: 2628

I solved the problem by reorganizing my code and keeping the sockets separated. For whatever reason, it seems that Access-Control-Allow-Origin is not needed. I am using a Chrome plugin called "Simple Web Socket Client" to get around needing to write my own client.

var ws = require("nodejs-websocket"),
    net = require("net");

var server = ws.createServer(function(conn) {
     conn.on("close", function(code, reason) {
        console.log("Connection closed");
    });
}).listen(8080);

var tcp = new net.Socket();
console.log('connecting to 127.0.0.1:13854');

tcp.connect(servicePort, '127.0.0.1', function() {

    //this socket requires sending data on connection
    tcp.write(JSON.stringify({
            enableRawOutput: false,
            format: "Json"
    }) + "\n");
});

tcp.on("data", function(data) {
    if (server == null || server.connections == null) {
        return;
    }

    //broadcast message:
    server.connections.forEach(function(conn) {
        conn.sendText(data);
    });
}

Upvotes: 1

Kurt Pattyn
Kurt Pattyn

Reputation: 2798

First of all, change the line

res.setHeader("Access-Control-Allow-Origin", "http://example.com");

to

res.setHeader("Access-Control-Allow-Origin", "*");

Because you are browsing to localhost, your request will be rejected because the origin is not http://example.com.

Secondly, in order to receive data, you must setup a web socket connection from the client. Just browsing to http://localhost:8080 creates an http connection and not a web socket connection. I propose to create an HTML page locally and use that by double-clicking on it (instead of going through your server); later you can host the page on your node.js server. Look at the examples on http://socket.io to correctly create a socket.io client.

Upvotes: 1

Related Questions