Giter Site home page Giter Site logo

Accessing Session in Websocket about modules HOT 8 CLOSED

totaljs avatar totaljs commented on September 3, 2024
Accessing Session in Websocket

from modules.

Comments (8)

darrudi avatar darrudi commented on September 3, 2024

in the meantime I came up with an implementation to do it:

in the websocket controller:

exports.install = function(framework) {
    framework.websocket('/', socket_homepage, ['json']);
};

function socket_homepage() {
  var controller = this;
  controller.on('open', function(client) {
    client.session = loadSessionByCookie(client);
    // logic ...
  }
}

function loadSessionByCookie(client) {
    if (!client || !client.req || !client.req.headers)
        return;

    var cookie = client.req.headers.cookie;

    if (!cookie)
        return;

    var id = client.cookie(framework.config.cookie_prefix) || '';

    if (id.length === 0) {
        return;
    }

    var obj = framework.decrypt(id, framework.config.cookie_secret);
    if (obj === null) {
        return;
    }

    if ('ssid_' + obj.sign !== cookieSignature(obj.id, client)) {
        return;
    }

    var session = framework.cache.read(obj.id);

    return session;
}

function cookieSignature(id, client) {
    return id + '|' + client.socket._peername.address.replace(/\./g, '') + '|' + client.req.headers['user-agent'].substring(0, USERAGENT).replace(/\s|\./g, '');
}

cookie_prefix and cookie_secret must be set in the config file and match exactly the values used by session.js.

the above code is a mimic of what happens in the session.js module. I am sure there are better ways to implement it; maybe by intercepting websocket requests and reusing session.js middleware (what happens to HTTP requests by default).

from modules.

petersirka avatar petersirka commented on September 3, 2024

Hi @darrudi,
sorry for my delay, I have been sick. What version of total.js do you use? I'll test the problem during the weekend and I'll give you a feedback.

Thanks and sorry.

from modules.

darrudi avatar darrudi commented on September 3, 2024

I am using version 1.6.0-1 (package.json)
For the moment I found a workaround as explained above. However it would be a good idea for the framework to provide the session in websocket connections by default.
Hope you get well as soon as possible.

from modules.

petersirka avatar petersirka commented on September 3, 2024

@darrudi Please download latest version from GitHub, it's really good.
I'll fix it. Thanks.

from modules.

petersirka avatar petersirka commented on September 3, 2024

Hi @darrudi, I fixed it. I found a problem in the framework and in the module. You must use total.js from GitHub (v1.7.0).

from modules.

darrudi avatar darrudi commented on September 3, 2024

I updated total.js to version 1700. Now the session is accessible for websocket requests. However the session modules raises the following error:
0017
it is the relevant code in the session module:

framework.middleware('session', function(req, res, next) {

        if (res.statusCode) {
            // classic HTTP
            res.once('finish', function() {
                session._write(req._sessionId, req.session);
            });
        } else {
            // websocket
            res.socket.on('close', function() {
                session._write(req._sessionId, req.session);
            });
        }

        session._read(req, res, next);

    });

it seems res is null for websocket connections. I can nest the if ... else ... block inside another if (res) { ... } however the session data is not refreshed after each response this way.

from modules.

petersirka avatar petersirka commented on September 3, 2024

Hi @darrudi,
I found a problem in the framework middleware.call(framework, req, null, next);. Can you test it? Please download latest version from GitHub. Thanks!

from modules.

darrudi avatar darrudi commented on September 3, 2024

I works perfectly Peter 👍
Thanks
By the way there was an error in Session.prototype._signature that seems to be resolved in the update (req.headers was null for websocket connections and threw and error when calculating the signature). Anyway it's ok now.

from modules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.