Mean.io框架与socket.io [英] Mean.io framework with socket.io

查看:185
本文介绍了Mean.io框架与socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Mean.io堆栈中使用socket.io?

How to use socket.io in Mean.io stack?

首先,Mean.io定期更改其文件夹结构..所以我的问题是是配置socket.io的最佳位置吗?或者最好使用express.io?

First of all, Mean.io changes their folder structure very regularly.. So my question is where is the best place to configure socket.io ? or is it better to use express.io ?

第二个我仍然不太确定在哪里寻找找到代码,告诉mean.io来听口岸,我已经在all.js文件中的config文件夹中找到了一个端口,但是真正的问题是一旦我定义server.listen(port)应用程序不加载。如果我没有应用程序加载,但是socket.io不起作用。

Second I am still not quite sure where to look for to find code that tells mean.io to listen for port, I have found a port is defined in config folder in all.js file, but real problem is as soon as I define server.listen(port) app doesn't load. and if I don't app loads but socket.io doesn't work.

另外我还有一个关于/socket.io/socket-io.js文件的问题?我已经在index文件夹中复制了,但是我的应用程序找不到或者说404错误。我知道这不是一个实际的文件坐在任何这样的位置,据我所知,也有人建议把该行为127.0.0.1/socket.io/socket-io.js,但没有使js文件可用于该应用程序可以运行socket.io。

Also I have another question about /socket.io/socket-io.js file? I have copied that in index folder, but my app can't find it or says 404 error. I know it's not an actual file sitting on any such location as far as I have understood, also people suggested to put that line as 127.0.0.1/socket.io/socket-io.js but none made the js file available for the app to be able to run socket.io.

在mean.io框架中定义socket.io的正确方法是什么?

What is the proper way of defining socket.io in mean.io framework?

推荐答案

我也面临同样的问题,花了我一个多星期才终于得到正确的结果。我会尝试解释我做了什么:

I also faced the same issue and took me about a week to finally get it right. I'll try to explain what I did:

app.js

在这个文件中,我只是调用了为我创建和设置一个socket.io对象的代码,然后传递给路由模块。

In this file, I just invoke the code that creates and sets up a socket.io object for me, which is then passed to the routes module.

'use strict';

/*
 * Defining the Package
 */
var Module = require('meanio').Module;

var MeanSocket = new Module('chat');

/*
 * All MEAN packages require registration
 * Dependency injection is used to define required modules
 */
MeanSocket.register(function(app, http) {

    var io = require('./server/config/socketio')(http);

    //We enable routing. By default the Package Object is passed to the routes
    MeanSocket.routes(io);

    return MeanSocket;
});

服务器/ config / socketio.js

此文件只是配置了socket.io对象。请注意,为了进行这项工作,我必须将平均值模块升级到版本0.5.26,因为在较旧的平均版本中,http 对象(快速服务器)不可用。此外,如果您想使用ssl,您可以注入 https 而不是 http

This file simply configures the socket.io object. Please note that I had to upgrade meanio module to version 0.5.26 for this work, as http object (express server) is not available in older meanio versions. Moreover, in case you want to use ssl, you can inject https instead of http.

'use strict';

var config = require('meanio').loadConfig(),
    cookie = require('cookie'),
    cookieParser = require('cookie-parser'),
    socketio = require('socket.io');

module.exports = function(http) {

    var io = socketio.listen(http);

    io.use(function(socket, next) {
        var data = socket.request;

        if (!data.headers.cookie) {
            return next(new Error('No cookie transmitted.'));
        }

        var parsedCookie = cookie.parse(data.headers.cookie);
        var sessionID = parsedCookie[config.sessionName];
        var parsedSessionID = cookieParser.signedCookie(parsedCookie[config.sessionName], config.sessionSecret);

        if (sessionID === parsedSessionID) {
            return next(new Error('Cookie is invalid.'));
        }

        next();
    });

    return io;
};

routes / chat.js

最后,使用routes文件定义套接字事件等。

Finally, use the routes file to define the socket events, etc.

'use strict';

// The Package is passed automatically as first parameter
module.exports = function(MeanSocket, io) {

    io.on('connection', function(socket) {

        console.log('Client Connected');

        socket.on('authenticate', function(data, callback) {

        });
    });
};

希望这有帮助!

这篇关于Mean.io框架与socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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