Comments (8)
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.
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.
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.
@darrudi Please download latest version from GitHub, it's really good.
I'll fix it. Thanks.
from modules.
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.
I updated total.js to version 1700. Now the session is accessible for websocket requests. However the session modules raises the following error:
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.
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.
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)
- How to control access in authorization HOT 6
- modules HOT 5
- Basic Stylus CSS renderer module HOT 1
- Broken demo link HOT 1
- auth whether already logged HOT 1
- Social Login Oauth HOT 2
- mail using sendgrid HOT 2
- Controller or view for 404 error HOT 2
- angular header creation HOT 1
- flash is not defined in conditionals? HOT 4
- auth.js HOT 2
- Typo in auth.js? HOT 1
- Using `@` flag for roles... HOT 1
- Jade/PUG - Global variables HOT 2
- Stylus module "CONFIG(...).indexOf is not a function" HOT 3
- Stylus @require and @import dont work
- the twitter module does not work HOT 3
- nosqlexplorer does not work as expected ... maybe I am wrong :-) HOT 6
- cnd.totaljs.com cdn down - 502 error HOT 2
- the pug module does not work HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modules.