簡體   English   中英

工作聊天示例,該示例使用會話對用戶進行身份驗證socket.io表達node.js

[英]working chat example which use session to authenticate users socket.io express node.js

有沒有一個可行的示例來存儲會話並將該會話用於所有打開的窗口,以使一個用戶僅一次連接到一個房間?

該應用程序將以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服務器

您的意思是在瀏覽器標簽之間共享一個websocket嗎?

跨瀏覽器標簽共享websocket?

但是,為什么需要共享套接字? 我為一個論壇開發了一個node.js聊天工具,我們不在乎用戶有多少個套接字。 我們只有一個具有套接字列表的“ User”對象。 我們不在乎套接字是否來自Firefox,Android應用...這不是問題。 當我們需要向用戶發送信息時,我們會將其發送到每個套接字。

你可以試試看 它使用express和socket.io https://github.com/gravityonmars/Balloons.IO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM