带有 socket.io 的 Mean.io 框架 [英] Mean.io framework with socket.io

查看:17
本文介绍了带有 socket.io 的 Mean.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 文件的问题?我已将其复制到索引文件夹中,但我的应用程序找不到它或显示 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;
});

server/config/socketio.js

这个文件只是配置了socket.io对象.请注意,我必须将 meanio 模块升级到 0.5.26 版才能完成这项工作,因为 http 对象(快速服务器)在较旧的 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;
};

routes/chat.js

最后使用routes文件定义socket事件等

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) {

        });
    });
};

希望这有帮助!

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

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