0
我現在知道,我原本打算MEMCACHE不工作,但我仍然不能夠發送用戶特定的數據(如的標識PHP和Node之間的成員)。如何PHP和節點之間發送用戶特定的數據與MEMCACHE或Redis的
目前爲止我只安裝了Memcache,但是如果這是我需要的,也可以安裝Redis,但是我需要一些方法來標識每個基於會話或cookie數據的登錄用戶。 (PHP)服務器,因爲他們登錄。
我寧願不使用一個通用的會話ID,然後用來引用一個數據庫,但我會,如果我必須這樣做。
這就是我是如何在PHP
$memcache = new Memcache;
$memcache->connect('localhost',11211);
$uData = json_encode($_SESSION);
$memcache->set('Session',$uData);
存儲,這是我原來的節點文件,這顯然將不得不因爲我誤解了內存緩存的功能可以顯著改變。
/*****************************************
* 1. Configuration
* 2. Declarations & Definitions
* 3. Memcache & Session Vars
* 4. Server Initiation
* 5. Client Connections
* 5-1. On Client Connect
* 6. Helper Functions
* 7. Utility Functions
*****************************************/
/*****************************************
* 1. Configuration
*****************************************/
// Basic config options
var NodePortNumber = 1337; // t3h 13370rz
var MemCachePortNumber = 11211; // default MemCache port is 11211
var MySQLParams = {
'Host': 'localhost',
'User': 'user',
'Pass': 'pass',
'DB': 'database'
};
/*****************************************
* 2. Declarations & Definitions
*****************************************/
var Express = require('express')();
var HTTP = require('http').Server(Express);
var IO = require('socket.io')(HTTP);
var MySQL = require('mysql');
var Database = MySQL.createConnection({
host : MySQLParams.Host,
user : MySQLParams.User,
password : MySQLParams.Pass,
database : MySQLParams.DB
});
/*****************************************
* 3. Memcache & Session Vars
*****************************************/
var MemCache, Session;
// Define a default value for all Session values so undefined warnings aren't thrown for non-members
Session = [];
Session['MemberID'] = 0;
Session['MemberEmail'] = undefined;
Session['MemberPass'] = undefined;
// Get session vars from Memcache
var Memcached = require('memcached');
MemCache = new Memcached('127.0.0.1:'+MemCachePortNumber); // connect to local memcached
MemCache.get('Session', function(err,result){
Session = JSON.parse(result);
});
/*****************************************
* 4. Server Initiation
*****************************************/
// Initiate the Node server
HTTP.listen(NodePortNumber, function(){
console.log('Server created. Socket listening on Port '+NodePortNumber);
});
/*****************************************
* 5. Client Connections
*****************************************/
var LiveClientCounter = 0; var Members = {};
var IsMember = false;
// Event fired every time a new client connects:
IO.sockets.on('connection', function (client) {
/*****************************************
* 5-1. On Client Connect
*****************************************/
var clientID = client.id;
LiveClientCounter++;
IO.emit('connection count', LiveClientCounter);
console.log('Member '+Session['MemberID'] +' connected');
if (typeof Session['MemberID'] != typeof undefined && Session['MemberID'] > 0) {
IsMember = true;
if (typeof Members[Session['MemberID']] == typeof undefined)
Members[Session['MemberID']] = [];
Members[Session['MemberID']][clientID] = {
SocketOBJ: client,
ClientID: clientID,
MemberID: Session['MemberID']
};
console.log('Key added for Member #'+Session['MemberID']);
}
});
您可以加入的如何傳輸這些數據的例子嗎?我在想一個GET變量,但似乎會有更好的方法。 – SISYN