Mean.io framwork与socket.io [英] Mean.io framwork with socket.io

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

问题描述

我一直在自己粘到我的计算机和互联网为过去几天关于如何使用socket.io在Mean.io堆栈妥善解决。我发现几个解决方案,但并不完全工作。

I have been gluing myself to my computer and internet for last couple of days for a proper solution on how to use socket.io in Mean.io stack. I have found few solutions but not quite working..

首先,Mean.io改变他们的文件夹结构非常有规律..所以我的问题是在哪里配置socket.io最好的地方?或者是它更好地使用前press.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 ?

二,我还是不太清楚到哪里寻找找到code,告诉mean.io侦听端口时,我发现了一个端口在config文件夹中定义的all.js的文件,但真正的问题是只要我定义server.listen(端口)的应用程序不会加载。如果我不应用负载,但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文件的另一个问题?我抄在索引文件夹,但我的应用程序无法找到它,或者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这将是一个巨大的帮助的正确方法。

If some one could help me to understand the proper way of defining socket.io in mean.io framework it would be a huge help..

我迫切需要一个答案。

感谢

推荐答案

我也面临着同样的问题,我花了大约一个星期,终于得到它的权利。我会尽力解释我做了什么:

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

在这个文件中,我只是调用code,创建,并设置了一个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;
});

服务器/配置/ socketio.js

该文件只配置socket.io对象。请注意,我不得不meanio模块升级到0.5.26版本这项工作,为的 HTTP 的对象(如preSS服务器)无法在旧版meanio版本。此外,如果你想使用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;
};

路线/ chat.js

最后,使用路由文件来定义套接字事件等。

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 framwork与socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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