我编写了一个Node.js socket.io例程,它将由我的raspberry pi中的python socket io例程调用。它将以两种方式进行沟通。当我在localhost上运行这两个例程时,它工作正常。但是,当我将服务器应用程序部署到cloudfoundry并将SocketIO连接链接更改为cloudfoundry时,它不起作用。下面是客户端python
from socketIO_client import SocketIO
def on_updatepi_response(*args):
print 'updatepi'
def on_receivepi_response(*args):
print 'receiveepi'
with SocketIO('raspinode-server.cloudfoundry.com', 8080) as socketIO:
socketIO.on('receivepi', on_receivepi_response)
socketIO.on('updatepi', on_updatepi_response)
socketIO.emit('sendrabbit','testdata')
socketIO.wait(seconds=1)
我知道cloudfoundry可能有点奇怪,因为我的第一个想法是使用rabbitmq,但它与VCAP_SERVICES的想法有关。但是我不认为在Node.js页面上存在这样的限制。
让我知道上述代码是否有任何问题,如果不能,我怎样才能让我的外部pi将阅读内容发送到我的云应用程序?
服务器代码如下所列,但不相关。它响应localhost ......我知道rabbitmq代码还没有连接起来
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var amqp = require('amqp');
var io = require('socket.io').listen(server)
function rabbitUrl() {
if (process.env.VCAP_SERVICES) {
conf = JSON.parse(process.env.VCAP_SERVICES);
return conf['rabbitmq-2.4'][0].credentials.url;
}
else {
return "amqp://localhost";
}
}
var port = process.env.VCAP_APP_PORT || 3000;
var messages = [];
function setup() {
var exchange = conn.exchange('cf-demo', {'type':'fanout', durable:false}, function(){
var queue = conn.queue('', {durable:false, exclusive:true},
function() {
queue.subscribe(function(msg) {
messages.push(htmlEscape(msg.body));
if (messages.length > 10) {
messages.shift();
}
});
queue.bind(exchange.name, '');
});
queue.on('queueBindOk', function() {httpServer(exchange);});
});
}
server.listen(8080);
io.sockets.on('connection', function(socket){
// when the client emits sendrabbit, this listens
socket.on('sendrabbit', function(data)
{
// we tell the client to execute updatepi with 2 parameters
io.sockets.emit('updatepi', socket.username, data)
});
socket.on('disconnect', function()
{
socket.broadcast.emit('updatepi', 'SERVER', socket.username + ' has disconnected');
});
});
答案 0 :(得分:1)
我的理解是您的服务器应该侦听端口Cloud Foundry分配它(在env var中可用)。您不能假设它将是8080.然后客户端与raspinode-server.cloudfoundry.com(没有端口)进行通信,Cloud Foundry将其路由到正确的位置。