此代码在localhost上完美运行,但在将其推送到云代工厂后无效...
这是服务器代码:
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
}
});
});
所以基本上我只是在等待开始连接的消息。之后,我将所有数据发送给客户端。
这是我客户的代码:
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);
});
});
在localhost上,这是结果:
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]}
但如果我将服务器放在云代工厂上,我就明白了:
client connected
message sended
然后停止......
我没有错误..没有。但在云代工厂我的调试消息说客户端连接不会出现! (console.log('New Connection')第38行......)
我有一些联系,但我想这只是云代工厂的保持系统..
有人有关于此的一些信息吗?
云代工刚刚说使用:var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000';
我想我必须在80端口绑定我的客户端,但我不确定...
谢谢
编辑: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',
答案 0 :(得分:0)
你应该记录
var port = process.env.VCAP_APP_PORT || process.env.PORT || '3000';
检查部署端口时使用的端口。
在客户端,发生的情况是端口可能是错误的(端口80)。
它没有在您的客户端显示任何错误,因为您没有打印它:
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);
});
});
可能发生的事情是它无法连接并发送消息,因此它会超时。
编辑:
在查看云代工厂文档之后,它写了流量重定向到端口80(http)或443(https)重定向到环境端口。
答案 1 :(得分:0)
我现在正在使用socket.io,一切都很完美 请参阅http://socket.io/和示例:http://socket.io/get-started/chat/
云代工厂的问题是一切都用过http!你还不能使用TCP ......他们正在研究它。