我想在像PHP这样的Node.js中使用SESSION

时间:2013-12-14 22:30:19

标签: javascript php node.js

*你好,我不懂英文嘛。请理解我。

我想使用PHP设置的会话数据,如登录信息。

韩国

로그인정보같이,PHP에서설정한세션정보를쓰고싶은데어떻해야할지잘모르겠어요。

代码

会话数据由PHP设置。

server.js:

var io = require("socket.io").listen(3000);

var load_history_count = 10;
var load_history_data = new Array();
// These Data is to load chat data by chatted others before access chat room.

io.sockets.on("connection", function(socket){
  for(var i=0;i<load_history_data.length;i++){
    socket.emit("getMsg", load_history_data[i]);
  }
  // These Code is to load chat data by chatted others before access chat room.

  socket.on("login", function(){
    socket.emit("getMsg", "This is your NickName:"+session.login_info['nickname']);
    // I want to use session data in here like session.login_info.nickname
    // or session.login_info['nickname'];
    // How can I use session data?
  });

  socket.on("sendMsg", function(data){
    load_history_data.push(data);
    if(load_history_data.length > load_history_count) load_history_data.shift();
    socket.broadcast.emit("getMsg", data);
    socket.emit("getMsg", data);
  });
});

client.js:

var url = "My Site Url";
var port = 3000;
var socket = io.connect(url+":"+port);

jQuery(function($){
    socket.emit("access");  
    socket.on("getMsg", function(data){
        $("<div>"+data+"</div>").appendTo($("#content"));
    });

    function sendMsg(){
        var a = $("#input");
        if(a.val() != ""){
            socket.emit("sendMsg", a.val());
            a.val("");
        }
    }

    $("#input").keydown(function(e){
        if(e.keyCode == 13) sendMsg();
    });

    $("#submit").click(function(){
        sendMsg();
    });
});

0 个答案:

没有答案