开发者

Socket.io and session data

开发者 https://www.devze.com 2023-03-10 10:58 出处:网络
Is this a suitable way of storing my temporary app data? socket.on(\'connection\', function(client){ client.myappsdata = {

Is this a suitable way of storing my temporary app data?

socket.on('connection', function(client){ 
  client.myappsdata = {
    a: true,
    b: false开发者_开发问答
  }
}


I prefer something slightly more heavy.

Pseudo-Code:

// ClientManager.js
var Manager = new function() {
    this._clients = [];

    this.set = function(client, data) {
        this._clients[client.sessionId] = data;    
    };

    this.get = function(client) {
        return this._clients[client.sessionId];            
    }

};

module.exports = function() {
    return Object.create(Manager);
};

// main.js
var manager = require("ClientManager")();

/* ... */

socket.on("connection", function(client) {
    manager.set(client, {
       /* ... */ 
    });
}

Bassically each client has a sessionId so store their data in a hash keyed by that sessionId

0

精彩评论

暂无评论...
验证码 换一张
取 消