Nodejs Socket挂断&ECONNRESET - 从 Meteor 到 Node js 服务器的 HTTP post 请求 [英] Nodejs Socket hang up & ECONNRESET - HTTP post request from Meteor to Node js server

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

问题描述

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

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

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

我向 node.js 服务器发出 HTTP post 请求以发送我的通知.

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

socket 在 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)
 在对象.<匿名>(packages/meteor/helpers.js:119:1)
 在 Object.HTTP.call (packages/meteorhacks_kadira/lib/hijack/http.js:10:1)
 在 Object.sendPushNotificationsMeteorServer (server/pushNotifications.js:249:1)
 在服务器/类/pushNotifications.js:244:1
 在 [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
 在包/流星/计时器.js:6:1
 在 runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)
 - - - - -
 在 createHangUpError (http.js:1473:15)
 在 Socket.socketOnEnd [as onend] (http.js:1569:23)
 在 Socket.g (events.js:180:16)
 在 Socket.emit (events.js:117:20)
 在 _stream_readable.js:944:16
 在 process._tickCallback (node.js:448:13)',详细信息:{[错误:套接字挂断]堆栈:[Getter]},数据:{[错误:套接字挂断]堆栈:[Getter]},用户:空,用户 ID:空,toString: [函数] },用户:空,用户 ID:空,toString: [功能] }

<前>错误:读取 ECONNRESET在 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)在 Object.call (packages/meteor/helpers.js:119:1)在 Object.sendHttpCall (server/pushNotifications.js:249:1)在服务器/pushNotifications.js:244:1在 [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)在包/流星/计时器.js:6:1在 runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)- - - - -在 errnoException (net.js:905:11)在 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 主机名 = 'http://localhost';无功端口 = 6000;var bodyParser = require('body-parser');nodeGcm = require('node-gcm');apn = require('apn');apnService = new apn.Connection(helperClass.getAPNOptions());//-- 身体解析器 --//app.use(bodyParser.json({limit: '50mb'}));app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));process.on('uncaughtException', function (err) {控制台错误(错误);console.log("节点未退出...");});//所有发布请求app.post('/', function(req, res){尝试 {var response = JSON.parse(req.body.pushNotificationApiParams);var callType = req.body.callType;开关(呼叫类型){案例'systemPushNotifications':返回 pushNotificationClass.sendPushNotificationsV2(response);休息;}}抓住(e){console.dir(e.stack);realFs.appendFile('errorLogs/'+helperClass.getCurrentDateFormated()+'.log', helperClass.formatLog('main Post Method 中的异常:'+e.stack) , function (err) {如果(错误)抛出错误;});}res.send("OK");});app.listen(端口,函数(){console.log('监听'+主机名+':'+端口);});

这是我在 Meteor 端的代码,我向节点 js 服务器发出 HTTP 发布请求:

var headers = {'内容类型':'应用程序/x-www-form-urlencoded'};var postFields = {callType : 'systemPushNotifications',pushNotificationApiParams : JSON.stringify(pushNotificationApiParams)//包含推送通知数据};HTTP.call("POST", 'http://localhost:6000', { params:postFields, headers:headers });

谁能指导我正确的方向?此外,我也非常感谢您了解一些好的做法.

还有

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

解决方案

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

之前:

switch (callType) {案例'systemPushNotifications':返回 pushNotificationClass.sendPushNotificationsV2(response);休息;}

现在:

switch (callType) {案例'systemPushNotifications':pushNotificationClass.sendPushNotificationsV2(响应);休息;}

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

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

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

My main app runs on the Meteor server.

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

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

socket hang up
    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)
    at Object.<anonymous> (packages/meteor/helpers.js:119:1)
    at Object.HTTP.call (packages/meteorhacks_kadira/lib/hijack/http.js:10:1)
    at Object.sendPushNotificationsMeteorServer (server/pushNotifications.js:249:1)
    at server/classes/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 createHangUpError (http.js:1473:15)
    at Socket.socketOnEnd [as onend] (http.js:1569:23)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:944:16
    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)

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

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.

Also

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.

解决方案

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:

Before:

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

Now:

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

The above return was terminating the code before the: res.send("OK");

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆