如何将属性关联到 Redis Store 中的 socket.io 对象? [英] How to associate properties to socket.io object in Redis Store?
问题描述
我正在使用 Nodejs 和 Socket.io 开发一个复杂的游戏,我需要在内存中存储 socket.io 对象,并将属性分配给 socket 对象(比如名称,来自 socket 的某些操作的计数器等)
I am working on a complex game with Nodejs and Socket.io, where I need to store socket.io objects in the memory and also assign properties to the socket object ( say a name , a counter of some action from the socket , etc )
在下面的代码中,我展示了我想要实现的目标的示例.我将所有套接字存储在一个数组中,并且还有另一个存储套接字名称属性的数组.在任何时候,如果我收到名称请求,我可以从内存中的数组中选择名称.
In the code below, I have shown an example of what I am trying to achieve. I store all the sockets in an array and also have another array which stores the name property of the socket. At any time if I get a request for the name, I can just pick the name from the array in the memory.
但是现在我的用户太多了,我需要在多个服务器之间平衡我的应用程序.所以我不能在内存中存储对象和属性.我需要将它们存储在数据库中.
But now I have too many users and I need to load-balance my application across multiple servers. So I cant store objects and properties in the memory. I need to store them in a Database.
我打算使用 Redis.此链接讲述了如何将 Redis Store 用于套接字 -
I am planning to use Redis. This link tells how to use Redis Store for sockets -
https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
但是我如何将我的其他属性(比如名称等)与 Redis 存储中的套接字对象相关联?如果有一些新的方法可以实现这一点,也请告诉我.
But how do I associate my other properties ( say name etc ) to the socket object in the Redis Store ? If there is some new ways to achieve this , please let me know also.
var socket_array = new Array();
var socket_name_array = new Array();
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket_array.push(socket);
var i = socket_array.indexOf(socket);
var name = generate_random_name();
socket_name_array[i]= name;
socket.on('get_name', function (data) {
var i = socket_array.indexOf(socket);
var name= socket_name_array[i]
socket.emit('socket_name' , {name :name } );
});
});
function generate_random_name(){
var random_string;
//code
return random_string;
}
推荐答案
是的,如果你想对 socket.io 服务器进行负载平衡,你将不得不使用像 redisstore 这样的存储.
Yes, if you want to load balance socket.io servers you will have to use a store like redisstore.
但是现在您不应该使用socket_name_array"+ 事件来保持整个服务器的数据一致.
However now you should not use "socket_name_array" + events to maintain data consistent across your server.
var redis = require('redis'),
var pub = redis.createClient(port, host),
var sub = redis.createClient(port, host),
var client = redis.createClient(port, host);
io.configure(function(){
io.set('store', new RedisStore({
redisPub: pub,
redisSub : sub,
redisClient : client
}));
});
用法
io.sockets.on('connection', function (socket) {
var name = generate_random_name();
socket.set('name', name); // store it in redis and forward this to other socket.io servers
// On another server, if you want to retrieve this value from this socket just do:
socket.get('name', function(err, name){
// don't forget err. handling
console.log(name);
});
});
这篇关于如何将属性关联到 Redis Store 中的 socket.io 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!