memcache + socket.io + php应用程序,用于读取会话和相互聊天

时间:2013-01-17 08:26:37

标签: php node.js socket.io chat

我在这个article中使用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选项卡连接时,他将连接到套接字服务器3或4次,如果他发布了一些数据我们有这个结果

master : hello
master : hello
master : hello

我希望用户只需连接一次我的应用程序,并且只能发布一次数据。现在我该如何实现?

如果互相发送私信,我应该如何访问这些用户

我是node.js.my bad.sorry的新手

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

1 个答案:

答案 0 :(得分:1)

1)你可以(似乎),var app = require('express')。express(); 2)在第一个app.get上,你不需要放2次JSON.parse,也许第二个JSON.parse不是你想要的(你试图检索用户扔那个字段吗?) 3)最重要的是:要使用房间,你必须使用socket.join加入一个房间,如果你没有 这样做,socket.broadcast将没有特效... 要从房间中删除用户,请使用socket.leave