域没有捕获错误/缺少错误处理程序

时间:2015-08-04 19:48:18

标签: javascript node.js error-handling

代码概述:
我尝试使用Redis合并Express和Socket.IO会话管理。似乎到目前为止工作,但我在使用域捕获错误时遇到了麻烦,如下面的测试所示。

服务器代码:

"use strict";

var domain = require("domain");
var redis = require("redis");
var express = require("express");
var app = express();
var server = require("http").createServer(app);
var socketio = require("socket.io");
var redisSio = require("socket.io-redis");

// Configure Express:
app.use(require("compression")());
app.use(require("body-parser").json());
var expressSession = require("express-session");
// Configure Socket.IO:
app.io = socketio(server, {
  transports: ["websocket", "polling"],
  // Same name as Express cookie:
  cookie: "test.sid"
});
// Manage Socket.IO sessions using Redis:
app.io.adapter(redisSio({socket: "/tmp/redis.sock"}));
// Manage Express sessions using Redis:
app.use(expressSession({
  // Same name as Socket.IO cookie:
  name: "test.sid",
  resave: false,
  saveUninitialized: false,
  secret: "secret",
  store: redis.createClient("/tmp/redis.sock", {
      return_buffers: true
    })
}));

var d = domain.create();
d.on("error", function(err) { console.log("Error"); });
d.run(function() {
  // Configure listener for Socket.IO connections:
  app.io.on("connection", d.bind(function(socket) {
    // Configure listener for specific event:
    socket.on("channel", d.bind(function(data, callback) {
      // "data" is general data incoming from client.
      // "callback" is a client function meant for acknowledgments.
      // Generate an error:
      null.test();
    }));
  }));
});

// Start the server:
server.listen(8080);

客户代码:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Test</title>
  <script language="javascript" type="text/javascript" src="/socket.io/socket.io.js"></script>
  <script type="text/javascript">
    var sock = io.connect();

    if(sock) {
      sock.emit("channel", null, function(data) {
        $("#tester").text(JSON.stringify(data));
      });
    }
    if(sock) {
      sock.disconnect();
      sock = null;
    }
  </script>
</head>
<body>
  <p id="tester"></p>
</body>
</html>

当从客户端接收事件(触发错误)时,以下内容将打印到控制台:

Missing error handler on `socket`.
TypeError: Cannot call method 'test' of null
    at Socket.<anonymous> (/home/debian/test.js:46:12)
    at Socket.b (domain.js:183:18)
    at Socket.emit (events.js:98:17)
    at Socket.onevent (/home/debian/node_modules/socket.io/lib/socket.js:330:8)
    at Socket.onpacket (/home/debian/node_modules/socket.io/lib/socket.js:290:12)
    at Client.ondecoded (/home/debian/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/home/debian/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/home/debian/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/home/debian/node_modules/socket.io/lib/client.js:175:18)
    at Socket.emit (events.js:95:17)

为什么域名无法捕获错误?为什么它抱怨socket上缺少错误处理程序?

相关软件包版本:

  • Node.js(v0.10.38)以及io.js(v2.5.0)
  • Express(4.13.3)
  • Socket.IO(1.3.6)

0 个答案:

没有答案