簡體   English   中英

Nodejs Socket掛斷&ECONNRESET - 從Meteor到Node js服務器的HTTP post請求

[英]Nodejs Socket hang up & ECONNRESET - HTTP post request from Meteor to Node js server

我正在使用節點服務器來處理我的所有推送通知服務,如gcm和apn。

我有2個不同的服務器。 一個運行Meteor,另一個運行Node.JS來處理推送通知。 (兩者都是不同的服務器)

我的主應用程序在Meteor服務器上運行。

我向node.js服務器發出HTTP post請求以發送我的通知。

通常它工作正常,但有時在Meteor服務器上,每當我調用node.js服務器時,我都會收到此錯誤:

socket hang up\n    at Object.Future.wait (/home/myPc/.meteor/packages/meteor-tool/.1.1.10.ki0ccv++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)\n    at Object.<anonymous> (packages/meteor/helpers.js:119:1)\n    at Object.HTTP.call (packages/meteorhacks_kadira/lib/hijack/http.js:10:1)\n    at Object.sendPushNotificationsMeteorServer (server/pushNotifications.js:249:1)\n    at server/classes/pushNotifications.js:244:1\n    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)\n    at packages/meteor/timers.js:6:1\n    at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)\n    - - - - -\n    at createHangUpError (http.js:1473:15)\n    at Socket.socketOnEnd [as onend] (http.js:1569:23)\n    at Socket.g (events.js:180:16)\n    at Socket.emit (events.js:117:20)\n    at _stream_readable.js:944:16\n    at process._tickCallback (node.js:448:13)',
details: { [Error: socket hang up] stack: [Getter] },
data: { [Error: socket hang up] stack: [Getter] },
user: null,
userId: null,
toString: [Function] },
user: null,
userId: null,
toString: [Function] }

要么

Error: read ECONNRESET
     at Object.Future.wait (/home/mbm/.meteor/packages/meteor-tool/.1.1.10.12ml1tp++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)
     at Object.call (packages/meteor/helpers.js:119:1)
     at Object.sendHttpCall (server/pushNotifications.js:249:1)
     at server/pushNotifications.js:244:1
     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
     at packages/meteor/timers.js:6:1
     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)
     - - - - -
     at errnoException (net.js:905:11)
     at TCP.onread (net.js:559:19)

這是我的Node.JS服務器代碼:

realFs                = require('fs');
var gracefulFs        = require('graceful-fs');
gracefulFs.gracefulify(realFs);

var http              = require('http');
var express           = require('express');
var app               = express();

var path              = require("path");

configClass           = require('./classes/config.js').configClass;
helperClass           = require('./classes/helper.js').helperClass;
pushNotificationClass = require('./classes/pushNotification.js').pushNotificationClass;

var hostname          = 'http://localhost'; 
var port              = 6000;

var bodyParser        = require('body-parser');

nodeGcm               = require('node-gcm');
apn                   = require('apn');
apnService            = new apn.Connection(helperClass.getAPNOptions());

// -- BODY PARSER -- //
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

process.on('uncaughtException', function (err) {

  console.error(err);
  console.log("Node NOT Exiting...");
});

// All post requests
app.post('/', function(req, res){

  try {

    var response = JSON.parse(req.body.pushNotificationApiParams);
    var callType = req.body.callType;

    switch (callType) {
        case 'systemPushNotifications':
            return pushNotificationClass.sendPushNotificationsV2(response);
        break;
    } 
  }
  catch(e){

    console.dir(e.stack);
    realFs.appendFile('errorLogs/'+helperClass.getCurrentDateFormated()+'.log', helperClass.formatLog('Exception in main Post Method  : '+e.stack) , function (err) {
      if (err) throw err;
    });
  }

  res.send("OK");
});



app.listen(port, function () {
  console.log('Listening at '+hostname+':'+port);
});

這是我的Meteor代碼,我向節點js服務器發出HTTP post請求:

var headers = {
   'Content-Type' : 'application/x-www-form-urlencoded'
};

var postFields = {
   callType : 'systemPushNotifications',
   pushNotificationApiParams  : JSON.stringify(pushNotificationApiParams)   // contains push notifications data                 
};

HTTP.call("POST", 'http://localhost:6000', { params:postFields, headers:headers });

誰能引導我朝着正確的方向前進? 我也非常感謝你也了解一些好的做法。

我還面臨一個問題。 我的node.js服務器在24小時后退出。 我不知道為什么會這樣。 它在終端控制台中沒有任何錯誤或異常退出。 我每次都要重啟它。

考慮到ECONNRESET錯誤通常發生在TCP connection的*另一側突然關閉時。

  • 在您的應用程序的情況下,它可能是由於服務器overloading並且簡單地將連接作為返回來殺死,其以類似的方式阻止與您的meteor server的連接

要獲得有關此線程中提到的錯誤的更多信息。 要處理錯誤,必須使用event listener來顯示它的完整stack traces

正如Farid Nouri Neshat在這篇帖子中提到的那樣

要為一組調用創建一個偵聽器,您可以使用域並在運行時捕獲其他錯誤。 確保與http(服務器/客戶端)相關的每個異步操作與代碼的其他部分相比處於不同的上下文中,域將自動偵聽錯誤事件並將其傳播到它自己的處理程序。 所以你只聽那個處理程序並獲取錯誤數據。

但由於域已被棄用,您應該使用文檔中提到的集群,這些集群使用server.listen(message)server.listen(handle)

或者您也可以使用NODE_DEBUG=net或使用strace

更新

對於服務器斷開連接,我認為錯誤可能在您處理bodyparser 。對於錯誤的 json文件, 錯誤未被捕獲

在未捕獲的異常之后,節點的默認操作是退出(崩潰)進程。

可以通過以下方式處理json文件的bodyparser。

var parseJson = bodyPaser.json();

app.use(function (req, res, next) {
    req.getBody = function (callback) {
        parseJson(req, res,function (err) {
          callback(err, req.body);
        });
    };
    next();
}); 

參考GITHUB開放問題在這里

更新2

基本上, socket hangup意味着套接字不會在指定的時間段內結束連接

根據源代碼,您可以看到, 如果服務器從未發送響應 ,則會發生這種情況

這個錯誤應該被捕獲和處理

  • 重試請求。
  • 稍后通過設置更多time period處理它,或者在函數結束時放入res.end()以結束連接。
  • 或者您可以將[http.get()][8]get請求一起使用,這將自動調用req.end()函數

希望它可能對你有所幫助! 干杯!

好的,我在這里找到了這個問題。 它在節點服務器代碼中。 我把return放在一個switch語句中,這不是一個在express中返回響應的有效方法,所以我只是從以下方法中刪除了返回:

之前:

switch (callType) {
   case 'systemPushNotifications':
      return pushNotificationClass.sendPushNotificationsV2(response);
   break;
}

現在:

switch (callType) {
   case 'systemPushNotifications':
      pushNotificationClass.sendPushNotificationsV2(response);
   break;
}

上面的return是在: res.send("OK");之前終止代碼res.send("OK");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM