简体   繁体   English

node.js + socket.io在heroku上不起作用

[英]node.js +socket.io is not working on heroku

Following part of my code is used for retrieving the data from TI sensor tag. 我代码的以下部分用于从TI传感器标签中检索数据。 So we are using sensortag node.js module to get the data and sending it to client using socket.io. 因此,我们正在使用sensortag node.js模块获取数据,并使用socket.io将其发送到客户端。 on local host the application is working fine but , when i push the code to heroku cloud web sockets part is not working. 在本地主机上,该应用程序运行正常,但是,当我将代码推送到heroku cloud web socket时,该部分无法正常工作。

Error : the server responded with a status of 400 (Bad Request) https://peaceful-plateau-6281.herokuapp.com/socket.io/?EIO=3&transport=polling&t=1449192192332-3 400 (Bad Request) 错误:服务器响应状态为400(错误请求) https://peaceful-plateau-6281.herokuapp.com/socket.io/?EIO=3&transport=polling&t=1449192192332-3 400(错误请求)

Following is my code : 以下是我的代码:

    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);
        });

And at the client side 在客户端

<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>

I am not getting the data at the client side through websockets.Can anybody please help me out. 我没有通过websockets在客户端获取数据。有人可以帮我吗。

Thanks&regards, Shivadeepthi 致谢,Shivadeepthi

I had the same error and just fixed. 我有同样的错误,只是解决了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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