Memcache + socket.IO + PHP应用程序读取会话和彼此聊天


memcache + socket.io + php application to read sessions and chat with each other

本文使用了mike为node.js编写的多房间聊天应用程序示例。并将其改为使用从PHP会话处理程序获取的会话数据,直到现在

这是我之前写的代码

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");
//check if user loggedin
// routing
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
  var cookieManager = new co.cookie(req.headers.cookie);
  var client = new memcache.Client(11211, "localhost");
  client.connect();
    user = client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
            var session = JSON.parse(result);
            user = JSON.parse(session.name);
            user = user.username;
            storeUsername(user);
    });
});
function storeUsername(user){
// usernames which are currently connected to the chat
var usernames = {};
io.of('/private').authorization(function (handshakeData, callback) {
  console.dir(handshakeData);
  handshakeData.foo = 'baz';
  callback(null, true);
}).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);

例如用户主将连接到我们的聊天室,他将有他的用户名存储从基于PHP的应用程序。但现在的问题在哪里?当用户从浏览器的2或3 TAB连接时他会连接到socket服务器3或4次如果他发布一些数据我们会得到这个结果

master : hello
master : hello
master : hello

我希望用户连接到我的应用程序只是一次,可以发布数据只是一次。我该怎么做呢?

我应该如何访问这个用户,以防彼此私信

i am so new in node.js.my bad.sorry

提前感谢您的帮助。所有教师+1

1)你可以你(似乎),var app = require('express').express();2)在第一个app.get,你不需要放2倍JSON。解析,可能是第二个JSON。解析不是您想要的(您是否试图检索用户抛出的字段?)最重要的是:要使用房间,必须使用插座。加入加入一个房间,如果你不加入的话去吧,插座。广播不会有什么特殊效果……要从房间中删除一个用户,使用socket.leave