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

node.js +socket.io is not working on heroku

本文关键字:heroku 运行 js+socket io node      更新时间:2023-09-26

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

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

以下是我的代码:

    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>

我没有通过网络套接字获取客户端的数据。有人能帮我吗。

谢谢&当做Shivadeepthi

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

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