本文介绍了Nodejs Socket挂断& ECONNRESET - 从Meteor到Node js服务器的HTTP发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用节点服务器处理我的所有推送通知服务,例如gcm和apn。

I am using a node server to handle all my push notifications services like gcm and apn.

我有2个不同的服务器。一个运行Meteor,另一个运行Node.JS来处理推送通知。 (两者都是不同的服务器)

I have 2 different servers. One is running Meteor and another is running Node.JS to handle push notifications. (Both are different servers)

我的主应用程序在Meteor服务器上运行。

My main app runs on the Meteor server.

我发了一条HTTP帖子请求node.js服务器发送我的通知。

I make an HTTP post request to the node.js server to send my notifications.

通常它工作正常,但有时在Meteor服务器上我每次调用node.js服务器时都会收到此错误:

Usually it works fine, but sometimes on the Meteor server I get this error whenever I call the node.js server:

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

OR


 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服务器代码:

Here is my Node.JS server code:

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请求:

And here is my code from Meteor side, where I am make HTTP post request to node js server:

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

任何人都可以指导我朝着正确的方向前进吗?此外,我也非常感谢您了解一些良好做法。

Can anyone guide me in the right direction? Also I would really appreciate to know some good practices as well.

同时

我还面临一个问题。我的node.js服务器在24小时后退出。我不知道为什么会这样。它在终端控制台中没有任何错误或异常退出。我每次都要重新启动它。

There is one more issue I am facing. My node.js server exits after like 24 hours. I don't know why does that happens. It exits without any error or exception in the terminal console. I have to restart it each time.

推荐答案

好的我在这里找到了这个问题。它在节点服务器代码中。我把return放在一个switch语句中,这不是一个在express中返回响应的有效方法,所以我只是从中删除了返回:

Ok I found the issue myself here. Its in the node server code. I put return in a switch statement which is not a valid way to return a response in express, so I just removed the return from:

之前:

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

现在:

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

以上返回正在终止代码: res.send(OK);

这篇关于Nodejs Socket挂断&amp; ECONNRESET - 从Meteor到Node js服务器的HTTP发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:04