1

我有下一个文件结构:

app.js - Server

data
    |__ config.txt   -  configuration
    |__ logs

srv
    |__ ticket.js    - ticket server
    |__ game.js      - game server

app.js 从“config.txt”读取服务器以加载和运行

配置文件

port=7891
servers=ticket

服务器也可以是:servers=ticket,game

票务.js

var init = function(io) {
    return io.of('/ticket')
    .on('connection', function (socket) {

        socket.on('message', function () { 

        });

        socket.on('disconnect', function () { 

        });

    });
};

module.exports = init;

应用程序.js

//
//  Requires
//
var _ = require('underscore')._;

var fs = require('fs');
var io = require('socket.io');
var logger = require('./mod/log.js');

//
//  Configuration
//

var options = {
    port: 6789,
    modules: [],
    servers: []
};

// Reads config.txt and insert values into "var options"
if(fs) {
    var config = fs.readFileSync("data/config.txt").toString().replace("\r\n", "\n").split("\n");
    var line, t, val;

    for(i in config) {
        if(config[i].indexOf("=") === -1)
            continue;

        line = config[i].split("=");
        t = Object.prototype.toString.call(options[line[0]]);
        t = t.slice(t.indexOf(" ")+1, t.length-1).toLowerCase();

        switch(t) {
            case 'number':
                val = parseInt(line[1]);
            break;

            case 'array':
                val = line[1].replace(", ", ",").split(",");
            break;

            default:
                val = line[1];
            break;
        }

        options[line[0]] = val;
    }

    logger.info("Configuration loaded");
    logger.info(options);

    io = io.listen(options.port);
}

// Starts all servers specified into config.txt
for(var i in options.servers) {
    logger.info("Starting server: "+options.servers[i]);

    var sv = require('./srv/'+ options.servers[i] + '.js');
    logger.info('./srv/'+ options.servers[i] + '.js');
    sv.init(io); //< HERE THROWS THE ERROR (*)

    logger.info("Ready");
}

(*) 错误是下一个:

2013-06-24T04:16:47.513Z - 错误:uncaughtException: 对象函数 (io) { return io.of('/ticket') .on('connection', function (socket) {

            socket.on('message', function () {

            });

            socket.on('disconnect', function () {

            });

    }); }  has no method 'init' date=Mon Jun 24 2013 01:16:47 GMT-0300 (Hora estándar de Argentina), pid=3316, uid=null, gid=null,

cwd=C:\Users\LUCIANO\Svn\Dropbox\Propi os\WebGamePlatform\trunk\Development\NodeJSServer, execPath=C:\Program Files (x8 6)\nodejs\node.exe, 版本=v0.10.8, argv= [节点,C:\Users\LUCIANO\Svn\Dropbox\Pro opios\WebGamePlatform\trunk\Development\NodeJSServer\app.js], rss=19689472, heap Total=10243584, heapUsed=5332216, loadavg=[0, 0, 0],正常运行时间=14388.1320351,跟踪=[列=5,文件=C:\Users\LUCIANO\Svn\Dropbox\Propios\WebGamePlatform\trunk\Deve lopment\NodeJSServer\app.js,函数=,行=60,方法=null,native=false,colum=26,file=module.js,function=Module._compile,line=456,method=_compile,native=false,column=10,file=module.js,function=Object .Module._extensions..js,行=474,方法=Module._extensions..js,native=false,列=32,文件=module.js,功能=Module.load,行=356,方法=加载,本机=假,列=12,文件=模块 .js,函数=Function.Module._load,行=312,方法=Module._load,本机=false,列=10,文件=模块.js,函数=Function.Module.runMain, line=497,method= Module.runMain,native=false,column=16,file=node.js,function=startup,line=11 9,method=null,native=false,column=3,file=node.js , function=null, line=901, m method=null, native=false], stack=[TypeError: Object function (io) { , return io.of('/ticket') ,stack=[TypeError: Object function (io) { , return io.of('/ticket') ,stack=[TypeError: Object function (io) { , return io.of('/ticket') ,
.on('connection', function (socket) { , ,
socket.on('message', function () { , , }); , ,
socket.on('disconnect', function () { , , }); , ,
});

我怎样才能解决这个问题?

4

1 回答 1

0

您正在导出一个函数,而不是一个对象。这意味着正确的调用应该是sv(io)而不是sv.init(io)
如果要公开该init功能,则必须使用module.exports.init = init.

于 2013-08-14T10:57:50.397 回答