【问题标题】:working chat example which use session to authenticate users socket.io express node.js使用会话验证用户的工作聊天示例 socket.io express node.js
【发布时间】:2013-02-15 02:19:21
【问题描述】:

是否有任何工作示例可以存储会话并将该会话用于所有打开的窗口,让一个用户只连接到一个房间一次?

这个应用程序将获取 phpsessions 作为 node.js 会话,但我不知道如何只让一个人访问这个聊天应用程序一次

//define what we need
    var express = require('express'),
        app = express(),
        memcache = require("memcache"),
        http = require('http'),
        server = http.createServer(app),
        io = require('socket.io').listen(server),
        co = require("./cookie.js"),
        php = require('phpjs'),
        codein = require("node-codein");


    // answer all request from users and send them back index.html for root access
    app.get('/', function (req, res) {
      res.sendfile(__dirname + '/index.html');
      var cookieManager = new co.cookie(req.headers.cookie);

      //using memcache as our session store
      var client = new memcache.Client(11211, "localhost");
      //connect to memcache client
      client.connect();
      //get our cookie sessions
        user = client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
                var session = JSON.parse(result);
                            //get just username from sessions(sessions store name and family and chat username in this case)
                user = JSON.parse(session.name);
                user = user.username;
                            //use this function to pass our chat username to our function
                storeUsername(user);
        });

    });

    function storeUsername(user){
    // usernames which are currently connected to the chat

    var usernames = {};
    io.sockets.on('connection', function (socket) {
        usernames[socket.id] = socket;
        // when the client emits 'sendchat', this listens and executes
        socket.on('sendchat', function (data) {
            // we tell the client to execute 'updatechat' with 2 parameters
            io.sockets.emit('updatechat', socket.username, data);
        });

        // when the client emits 'adduser', this listens and executes
        socket.on('adduser', function(username){
            // we store the username in the socket session for this client
            socket.username = user;
            // add the client's username to the global list
            // echo to client they've connected
            if(php.in_array(socket.username,usernames)){
                delete usernames[socket.username];
            }else{
                usernames[user] = user;
                console.log('not exist');
            socket.emit('updatechat', 'SERVER', 'you have connected');
            // echo globally (all clients) that a person has connected
            socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
            // update the list of users in chat, client-side
            io.sockets.emit('updateusers', usernames);
            }
        });

        // when the user disconnects.. perform this
        socket.on('disconnect', function(){
            // remove the username from global usernames list
            delete usernames[socket.username];
            // update list of users in chat, client-side
            io.sockets.emit('updateusers', usernames);
            // echo globally that this client has left
            socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
        });
    });
    }
    server.listen(3000);

一切正常,并且定义了发送数据的用户,但是当我在另一个选项卡中访问此站点时,我将再次连接到 socket.io 服务器

【问题讨论】:

    标签: node.js session express socket.io


    【解决方案1】:

    你的意思是在浏览器标签之间共享一个 websocket?

    Sharing websocket across browser tabs?

    但是为什么需要共享套接字呢?我为论坛开发了一个 node.js 聊天,我们不关心用户有多少个套接字。我们只有一个包含套接字列表的“用户”对象。我们不在乎套接字是否来自 Firefox,来自 android 应用程序......这不是问题。而当我们需要向用户发送信息时,我们会将其发送到每个套接字。

    【讨论】:

    • 问题是当用户登录并打开许多选项卡时,当他输入内容并按下回车键时,我的代码会出现许多聊天对话框。现在在这种情况下我应该在哪里编辑我的代码?
    • 在我的情况下,当用户按下回车键时,消息被发送到服务器。服务器将消息发送回作者(以及其他用户),然后每个客户端将消息写入对话。这样您就不会在对话中多次看到该消息。不知道这是不是你需要的。您是在谈论私人消息(从一个用户到另一个用户)、全局喊话框?
    【解决方案2】:

    你可以试试这个。它正在使用 express 和 socket.io https://github.com/gravityonmars/Balloons.IO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 2014-02-10
      • 2015-03-14
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      相关资源
      最近更新 更多