简体   繁体   中英

How to connect my client to a node.js server on cloud foundry

This code works perfectly on localhost but doesn't work after pushing it on cloud foundry...

this is the server code :

var net = require("net"),
    JsonSocket = require("json-socket");

var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000';
var server = net.createServer();

//DEFINE A JSON MODEL FOR TAG DATAS
var tags = [];
var ids = [1, 2, 3, 4, 5];
var date = new Date();
for (var i = 0; i < ids.length; i++) {
    tags.push({id: ids[i],
               timestamp: date,
               position: [0, 0]
           });
    console.log(tags[i]);
}

function update(tags, i){
    var now = new Date();
    tags[i].timestamp = now;
    if(tags[i].position[0] < 180 && tags[i].position[0] > -180){
        tags[i].position[0] += Math.floor(Math.random()*3 - 1);
    } else {
        tags[i].position[0] = 0;
    }
    if(tags[i].position[1] < 180 && tags[i].position[1] > -180){
        tags[i].position[1] += Math.floor(Math.random()*3 - 1);
    } else {
        tags[i].position[1] = 0;
    }
}

server.listen(port);
console.log('Socket emitting on port: '+port);

server.on("connection", function(socket) {
    console.log('New connection');
    socket = new JsonSocket(socket);
    console.log('JsonSocket created');
    var i = 0;
    socket.on("message", function(message) {
        console.log('Mesaage received');
        if(message.command == "newData"){
            //remplir l'array
            //message.data.position[0] --> x
        }else{
            streamInterval = setInterval(function() {
                if(i<tags.length){
                   socket.sendMessage(JSON.stringify(tags[i]));
                   update(tags, i);
                   i++;
                } else {
                    i = 0;
                    socket.sendMessage(JSON.stringify(tags[i]));
                    update(tags, i);
                    i++;
                }
            }, 1000); //SET INTERVAL DURATION IN MS
        }
    });
});

So basically i'm just waiting for a message starting a connection. After that I send all my data to the client.

This is the code of my client :

var net = require('net'),
    JsonSocket = require('json-socket');

var port = 80; 
var host = 'locationserver.apps.eu01.cf.canopy-cloud.com';
var socket = new JsonSocket(new net.Socket());

socket.connect(port, host);

socket.on('connect', function(err) { 
    console.log('client connected');
    socket.sendMessage({command: 'start'});
    console.log('message sended');
    socket.on('message', function(msg) {
        console.log(msg);
    });
});

On localhost this is the result :

client connected
message sended
{"id":1,"timestamp":"2016-04-12T12:36:33.491Z","position":[0,0]}
{"id":2,"timestamp":"2016-04-12T12:36:33.491Z","position":[0,0]}
{"id":3,"timestamp":"2016-04-12T12:36:33.491Z","position":[0,0]}
{"id":4,"timestamp":"2016-04-12T12:36:33.491Z","position":[0,0]}
{"id":5,"timestamp":"2016-04-12T12:36:33.491Z","position":[0,0]}

But if I put the server on cloud foundry i get this :

client connected
message sended

And then it stop...

I have no error.. nothing. But on cloud foundry my debug message saying that a client is connected doesn't appear ! (console.log('New Connection') line 38)...

I have some connection but i suppose it's just the keep alive system of cloud foundry..

Does someone have some information about this ? cloud foundry just said to use : var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000'; var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000'; And i suppose I then have to bind my client on port 80 but i'm not sure of that...

Thank you

EDIT : content of process.env

Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT PWD: '/home/vcap/app',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT NODE_HOME: '/home/vcap/app/.heroku/node',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT MEMORY_LIMIT: '1024m',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT CF_PROCESS_TYPE: 'web',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT VCAP_APP_HOST: '0.0.0.0',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT PORT: '61231',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT CF_INSTANCE_PORTS: '[{"external":61231,"internal":61231}]',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT HOME: '/home/vcap/app',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT CF_INSTANCE_INDEX: '0',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT CF_INSTANCE_IP: '10.0.2.54',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT VCAP_SERVICES: '{}',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT NODE_ENV: 'production',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT CF_INSTANCE_PORT: '61231',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT LANG: 'en_US.UTF-8',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT MEMORY_AVAILABLE: '1024',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT WEB_CONCURRENCY: '2',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT PATH: '/home/vcap/app/.heroku/node/bin:/bin:/usr/bin:/home/vcap/app/bin:/home/vcap/app/node_modules/.bin',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT VCAP_APPLICATION: '{"limits":{"mem":1024,"disk":1024,"fds":16384},"application_id":"28c1b6b7-230c-4d54-9954-3b837e65b128","application_version":"b835475f-7205-4f27-abc6-ea125c600191","application_name":"locationServer","application_uris":["locationserver.apps.eu01.cf.canopy-cloud.com"],"version":"b835475f-7205-4f27-abc6-ea125c600191","name":"locationServer","space_name":"Agile Unit","space_id":"19879db7-9f82-4ccf-93d4-31ce7a1f9e72","uris":["locationserver.apps.eu01.cf.canopy-cloud.com"],"users":null,"instance_id":"0dbb0c8e138d4d6b869b1ac6fcf4b66b","instance_index":0,"host":"0.0.0.0","port":61231,"started_at":"2016-04-12 13:27:37 +0000","started_at_timestamp":1460467657,"start":"2016-04-12 13:27:37 +0000","state_timestamp":1460467657}',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT USER: 'vcap',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT VCAP_APP_PORT: '61231',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT _: '/home/vcap/app/.heroku/node/bin/node' }
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT WEB_MEMORY: '512',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT TMPDIR: '/home/vcap/tmp',
Tue Apr 12 2016 13:27:39 GMT+0000 (UTC) App/0   OUT { CF_INSTANCE_ADDR: '10.0.2.54:61231',

You should log var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000'; var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000'; to check which port is used when you deploy it.

On your client side, what happens is that the port is probably wrong (port 80).

It doesn't show any error on your client side because you didn't print it :

socket.on('connect', function(err) { 
    if (err) {
      console.log(err);
    }
    console.log('client connected');
    socket.sendMessage({command: 'start'});
    console.log('message sended');
    socket.on('message', function(msg) {
        console.log(msg);
    });
});

What probably happens is that it can't connect and send the message, so it goes on timeout.

EDIT :

After looking at cloud foundry docs, it's written that the traffic is redirect going to port 80 (http) or 443 (https) is redirected on the environment port.

I'm now using socket.io and everything works perfectly see at http://socket.io/ and an exemple : http://socket.io/get-started/chat/

The problem with cloud foundry is that Everything used http ! You can't use TCP on it yet.. they are working on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM