0

セッションを保存し、開いているすべてのウィンドウでそのセッションを使用して、1 人のユーザーが 1 つの部屋に 1 回だけ接続できるようにする実際の例はありますか?

このアプリは、phpsessions を node.js セッションとして取得しますが、1 人のユーザーがこのチャット アプリケーションに 1 回だけアクセスできるようにする方法がわかりません。

//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 サーバーに接続します

4

2 に答える 2

1

あなたはこれを試すことができます。expressとsocket.ioを使用していますhttps://github.com/gravityonmars/Balloons.IO

于 2013-02-14T18:19:21.730 に答える
1

ブラウザのタブ間でWebSocketを共有することを意味しますか?

ブラウザタブ間でWebSocketを共有しますか?

しかし、なぜソケットを共有する必要があるのでしょうか。フォーラム用にnode.jsチャットを開発しましたが、ユーザーが持っているソケットの数は気にしません。ソケットのリストを持つ「User」オブジェクトがあります。ソケットがFirefoxからのものか、Androidアプリからのものかは関係ありません...問題ありません。また、ユーザーに情報を送信する必要がある場合は、各ソケットに送信します。

于 2013-01-18T20:07:56.343 に答える