【问题标题】:Serve/Stop serve Socket.IO服务/停止服务 Socket.IO
【发布时间】:2016-05-05 22:26:12
【问题描述】:

我想知道如何仅为登录用户提供 socket.io?

现在我只是添加/删除

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io();
</script>

但是当我在会话成功后删除它时,页面没有加载。仅对具有护照会话身份验证的用户使用/服务 socket.io 有什么想法吗?

【问题讨论】:

  • 你看过this answer吗?
  • 你为什么想做这样的事情?这是一个开源库,任何人都可以从任何地方获取。如果您询问如何只允许注册用户连接到您的套接字服务器 - 这是完全不同的。
  • @mef 它以某种方式满足了我的需求,但我确信有可能为用户简单地打开/关闭插座。目前我注意到,如果我将 /socket.io/socket.io.js 添加到每个站点并且 var socket = io(); 将打开/关闭,一切正常,服务器不使用套接字单元我打开它。
  • @AndreyPopov - 这就是重点。只有当用户为了服务器资源而登录时,我才想打开套接字。我不想/不需要为每个匿名查看我的网站的人打开套接字。有什么想法吗?
  • 服务器返回的http状态码是什么,是页面的html渲染不显示,还是整个&lt;body&gt;为空?

标签: node.js websocket socket.io passport.js passport-local


【解决方案1】:

这里真正的答案是使用SocketIO 框架中所谓的handshake。它允许您进行一些检查并决定是否应该允许用户连接到您的服务器。周围的其他答案根本不会自动允许用户连接。但是,如果他只打开一个控制台并针对您的服务器实例化套接字 - 他就在线。

看看这个:http://socket.io/docs/server-api/#namespace#use(fn:function):namespace

在每次连接尝试时,您都可以运行特定函数来查看是否正常。然后,您可以拒绝连接(使用参数调用 next),或接受它 - 只需调用 next

就是这样:)

但棘手的部分来了——如何真正验证用户身份?每个套接字都使用来自客户端的简单 HTTP 请求进行实例化。后来升级为socket连接。

如果您使用某种数据库或会话,您可以使用其中的许多模块。我一直在使用护照,所以一切都会自动发生。以下是有关如何操作的更多信息:https://github.com/jfromaniello/passport.socketio

var io           = require("socket.io")(server),
sessionStore     = require('awesomeSessionStore'), // find a working session store (have a look at the readme)
passportSocketIo = require("passport.socketio");

io.use(passportSocketIo.authorize({
    cookieParser: cookieParser,       // the same middleware you registrer in express
    key:          'express.sid',       // the name of the cookie where express/connect stores its session_id
    secret:       'session_secret',    // the session_secret to parse the cookie
    store:        sessionStore,        // we NEED to use a sessionstore. no memorystore please
    success:      onAuthorizeSuccess,  // *optional* callback on success - read more below
    fail:         onAuthorizeFail,     // *optional* callback on fail/error - read more below
}));

function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');

    // The accept-callback still allows us to decide whether to
    // accept the connection or not.
    accept(null, true);

    // OR

    // If you use socket.io@1.X the callback looks different
    accept();
}

function onAuthorizeFail(data, message, error, accept){
    if(error)
        throw new Error(message);
    console.log('failed connection to socket.io:', message);

    // We use this callback to log all of our failed connections.
    accept(null, false);

    // OR

    // If you use socket.io@1.X the callback looks different
    // If you don't want to accept the connection
    if(error)
        accept(new Error(message));
    // this error will be sent to the user as a special error-package
    // see: http://socket.io/docs/client-api/#socket > error-object
}

【讨论】:

  • passport.socketio - 完全赢了!!!谢谢你,小伙伴!我目前正在使用带有 postgres 的本地护照策略来维护身份验证。使用 passport.socketio,它会非常适合!
【解决方案2】:

将参数传递给您的视图引擎

例如,我在这里使用车把 -

app.get('/view', ...
    res.render('view.html', { authenticated : true } );

在你看来

{{#if authenticated }}
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io();
</script>
{{/if}}

这不会以任何方式保护您的服务器

你也可以这样做——

<script src="/socket.io/socket.io.js"></script>
<script>
    var connect = {{authenticated}} ;
    if(connect) {
       var socket = io();
    }

</script>

【讨论】:

  • 这正是我现在正在做的事情。但是如何保护服务器呢?另外:假设我想让人们在我的聊天中写字。现在我如何向服务器保证某些用户不会更改他的 ID(如果我不显示它,如何从 js 获取它???)并更改在他的消息下显示的登录名?
  • 在数据中传递一些令牌。验证令牌是否与存储在服务器中的令牌与用户会话匹配。
  • 不幸的是,这并不能阻止任何人在控制台中写入 io() 并且一切都会启动:)
猜你喜欢
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 2012-06-22
相关资源
最近更新 更多