简体   繁体   中英

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

Following part of my code is used for retrieving the data from TI sensor tag. So we are using sensortag node.js module to get the data and sending it to client using 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.

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)

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.

Thanks&regards, 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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