模块nodjs之间进行通信

时间:2016-07-31 03:49:35

标签: javascript node.js sockets

我在文件中有一个快速模块来注册用户名,在另一个文件中有socket.io模块来创建聊天。什么是将用户名发送到套接字模块的最佳方式?

server.js

var express = require('express');
app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var morgan = require('morgan');
var wagner = require('wagner-core');
var bodyParser = require('body-parser');

var port = process.env.PORT || 8080;

var name = [];

app.use(morgan('dev'));
app.use(express.static('public'));

app.use(bodyParser.urlencoded({'extended':'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json

var dependencies = require("./app/dependencies.js")(wagner);

require('./app/routes.js')(app,wagner);

require('./app/socketio.js')(io,wagner);

http.listen(port,function(){
    console.log('listening on *:'+port);
});

./应用程序/ routes.js

module.exports = function(app,wagner) {

    // api ------------------------------------------------------------------

    app.post('/api/user', function(req, res) {
        console.log(req.body);
        // name.push(req.body.name);
        res.json({ message: 'from post' });
        wagner.invoke(function (UserName){
            UserName = "mjt";
        });
    });
};

./应用程序/ socketio.js

module.exports = function (io,wagner) {

    io.on('connection', function(socket){
        var userName = wagner.invoke(function(UserName) {
            console.log(UserName); 
        });

        console.log('a user connected');
        io.emit('user connected', "connected");

        socket.on('chat message', function(msg) {
            console.log('message: ' + msg);
            io.emit('chat message', msg);
        });

        socket.on('disconnect',function () {
            console.log('user disconnect');
            io.emit('user disconnected', "disconnected");
        });
    });
}

1 个答案:

答案 0 :(得分:1)

找到一种通过使模块单例传递数据的方法,这可能不是最佳解决方案,但我可以实现我想要的

./应用程序/ socketio.js

var socketio = function () {}

socketio.prototype.users = [];

socketio.prototype.start = function(io){
            console.log("from start");
    var that = this.users;
    var people = {};
    io.on('connection', function(socket){
        console.log(that);

        console.log('a user connected');
        io.emit('user connected', "connected");

        socket.on('chat message', function(msg) {
            console.log('message: ' + msg);
            io.emit('chat message', msg);
        });

        socket.on('disconnect',function () {
            console.log('user disconnect');
            io.emit('user disconnected', "disconnected");
        });
    });
    }

socketio.prototype.updateUser = function (user) {
    console.log(user);

    this.users.push(user);
        console.log(this.users);
}
module.exports = exports = new socketio();

server.js

var express = require('express');
app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var morgan = require('morgan');
var wagner = require('wagner-core');
var bodyParser = require('body-parser');

var port     = process.env.PORT || 8080;


var name = [];

var socketIo = require('./app/socketio.js');

var startio = socketIo.start(io);
socketIo.updateUser("hey");
socketIo.updateUser("there");

app.use(morgan('dev'));
app.use(express.static('public'));

app.use(bodyParser.urlencoded({'extended':'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json

var dependencies = require("./app/dependencies.js")(wagner);

require('./app/routes.js')(app,wagner,socketIo);

http.listen(port,function(){
  console.log('listening on *:'+port);
});  

希望这有助于像我这样的需要,请提出更好的方法,谢谢