node.js + socket.io无法在heroku上运行

时间:2015-12-04 01:33:51

标签: javascript node.js heroku websocket sensor

以下部分代码用于从TI传感器标签中检索数据。所以我们使用sensortag node.js模块来获取数据并使用socket.io将其发送到客户端。在本地主机上应用程序工作正常但是,当我将代码推送到heroku云网络套接字部分不起作用。

错误:服务器响应状态为400(错误请求) https://peaceful-plateau-6281.herokuapp.com/socket.io/?EIO=3&transport=polling&t=1449192192332-3 400(错误请求)

以下是我的代码:

    var express = require('express');
    var port = process.env.PORT || 3000;
    var app = module.exports.app = express();
    var server = require('http').Server(app);
    //var io = require('socket.io')(server);
    var SensorTag = require('sensortag');
    var path = require('path');

var io = require('socket.io').listen(server.listen(port,function(){

console.log("We have started our server on port " + server.address().port);
// SensorTag.discover(function(tag) { and close it with }); above ondiscover mthod
function onDiscover(tag){

    tag.on('disconnect', function() {
        console.log('disconnected!');
        process.exit(0);
    });

    function connectAndSetUpMe() {          // attempt to connect to the tag
        console.log('connectAndSetUp' + tag.id);
        tag.connectAndSetUp(enableDataPoints);  // when you connect, call enableIrTempMe

    }

    function enableDataPoints(){
        console.log('enabling Temp datapoint');
        tag.enableIrTemperature(notifyMe);
        tag.enableHumidity(notifyHumd);
        tag.enableBarometricPressure(notifyPress);
        tag.enableAccelerometer(notifyAccel);
    }   

    function notifyMe(){
        console.log("notifying temp datapoints");
        tag.notifyIrTemperature(listenForReading);
    }
    function notifyHumd(){
        console.log("notifying humd datapoints");
        tag.notifyHumidity(listenForHumdReading);
    }
    function notifyPress(){
        console.log("notify pressure");
        tag.notifyBarometricPressure(listenForPress);
    }
    function notifyAccel(){
        console.log("notify Accerlerometer");
        tag.notifyAccelerometer(listenForAcc);
    }

    function  listenForReading(){       
        tag.on('irTemperatureChange', function(objectTemp, ambientTemp) {

            console.log('\tObject Temp = %d deg. C', objectTemp.toFixed(1));
            function TempChange() {
                io.sockets.emit('objTemp', { sensorId:tag.id, objTemp: objectTemp, ambTemp: ambientTemp});
            };
                TempChange();    

            });
        }
    connectAndSetUpMe();
        }
        SensorTag.discover(onDiscover);
    })
    );
    io.on('connection', function () {
          io.set("transports", ["xhr-polling"]);
          io.set("polling duration", 10);
        });

在客户端

<head>
<script src='/socket.io/socket.io.js'></script>
<script>
<script>  
        var socket = io.connect("\/\/"+window.location.hostname+":"+location.port);
           //var socket = io.connect(window.location.hostname);
            console.log("window.location.hostname"+location.port);
            socket.on('objTemp', function(data) {
                $('#objTemp').html(parseInt(data.objTemp));
                console.log("This is my places");
                $('#ambTemp').html(parseInt(data.ambTemp));

</script>
</head>
<body>
<p id="objTemp"></p>
</body>
</html>

我没有通过websockets在客户端获取数据。任何人都可以帮助我。

感谢和安培;问候, Shivadeepthi

1 个答案:

答案 0 :(得分:0)

我有同样的错误,只是修复了。

var io = require('socket.io').listen(server);
io.set('origins', '*:*');
io.set('match origin protocol', true);