2015-12-04 81 views
0

我的代碼的以下部分用於從TI傳感器標籤中檢索數據。所以我們使用sensortag node.js模塊來獲取數據並使用socket.io發送給客戶端。在本地主機上應用程序工作正常,但是,當我將代碼推到Herokuku雲網絡套接字部分不起作用。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

回答

0

我有同樣的錯誤,只是固定。

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

歡迎來到Stack Overflow!請考慮解釋你提供的代碼。 – Victor

相關問題