javascript - New to nodejs. what is the equivalent of PHP's session_start() in nodejs? -
i generate session id, have unique browser, not per tab.
all able find type of stuff >>
http://blog.modulus.io/nodejs-and-express-sessions
which demonstrates how use sessions store data, limited in each tab ends new session. looking for, browser gets unique session id generated automatically , accessible other tabs. trying stay away browser storage until has matured point users have feature. since still have quite number of users using older browsers not support local storage, random session id ideal.
i using unique id private 'room' users of chat program join support. each user in own private room, can access same room across tabs. know it's bit sloppy, beginner in nodejs's perspective simpler use unique key browsers instance deal cross-tab socket open/close detection, etc. realize using method, each tab have it's own socket connection server ok -- now.
i have tried following (which looks me use sockets session, , not browser session if did work), doesn't work (probably obvious experienced why doesn't work.
client
socket.on('connect', function() { socket.emit('set room', this.socket.sessionid); })
server
socket.on('set room', function(room) { socket.join(room); console.log('joined : ' + room); });
here simple script example, , whats happening using answer provided use express-sessions
. isn't playing socket.io, or gone terribly wrong:
(removed app.use(function.....)
session since causing client never reach socket connection.
app.js
var express = require('express'), app = express(), server = require('http').createserver(app), io = require('socket.io').listen(server), session = require('express-session'); var settings = require('./settings.json'); server.listen(settings.port); app.use(session({ secret: settings.secret, resave: false, saveuninitialized: true })); //app.use(function(req, res, next) { // var sess = req.session; // if(!sess.id) { // sess.id = sess.sessionid; // } //}); app.get('/', function(request, response){ validatehost('index.html', request, response); }); io.sockets.on('connection', function(socket){ socket.on('send message', function(data){ //io.sockets.emit('new message', data); console.log(session.sessionid); io.sockets.emit('new message', 'session:' + session.sessionid); socket.broadcast.to(socket.room).emit('new message', data); }); socket.on('set room', function(room) { socket.join(room); console.log('joined : ' + room); }); socket.on('disconnect', function(){ console.log('user disconnected'); }); }); function validatehost(file, request, response) { var hostname = ( request.headers.host.match(/:/g) ) ? request.headers.host.slice( 0, request.headers.host.indexof(":") ) : request.headers.host; // var file = 'index.html'; if(settings.hosts.indexof(hostname)) { response.sendfile(__dirname + '/' + file); } else { response.statuscode = 404; response.write('access denied ' + request.headers.host + ' ' + file); response.end(); } }
index.html
<html> <head> <title>realtime private chat</title> <style> #chat { height: 200px; width: 300px; overflow-y: auto; overflow-x: hidden; } #chat p { width: 270px; } .commentarea { font: 14px arial; padding: 0 10px; margin-top: 20px; } .bubbledleft,.bubbledright { margin-top: 20px; padding: 5px 9px; max-width: 50%; clear: both; position: relative; } .bubbledleft{ float: left; margin-right: auto; -webkit-border-radius: 8px 8px 8px 0px; -moz-border-radius: 8px 8px 8px 0px; -o-border-radius: 8px 8px 8px 0px; -ms-border-radius: 8px 8px 8px 0px; border-radius: 8px 8px 8px 0px; background-color: #65b045; color: #ffffff; } .bubbledleft:before { border-bottom: 10px solid #65b045; border-left: 9px solid rgba(0, 0, 0, 0); position: absolute; bottom: 0; left: -8px; content: ""; } .bubbledright{ float: right; margin-left: auto; text-align: right; -webkit-border-radius: 8px 8px 0px 8px; -moz-border-radius: 8px 8px 0px 8px; -o-border-radius: 8px 8px 0px 8px; -ms-border-radius: 8px 8px 0px 8px; border-radius: 8px 8px 0px 8px; background-color: #07d; color: white; } .bubbledright:before { border-bottom: 9px solid #07d; border-right: 9px solid rgba(0, 0, 0, 0); position: absolute; bottom: 0; right: -8px; content: ""; } </style> </head> <body> <div id="chat"></div> <form id="send-message"> <input id="message" type="text" /> <button type="submit">send</button> </form> </body> <script src="//code.jquery.com/jquery-latest.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script> jquery(function($) { var socket = io.connect(); var sendmessage = $('#send-message'); var sendmessagetext = $('#message'); var chat = $('#chat'); sendmessage.submit(function(e) { e.preventdefault(); socket.emit('send message', sendmessagetext.val()); chat.append('<div class="bubbledright">' + sendmessagetext.val() + '</div>'); sendmessagetext.val(''); }) socket.on('connect', function() { //socket.emit('set room', this.socket.sessionid); }) socket.on('new message', function(data) { chat.append('<div class="bubbledleft">' + data + '</div>'); chat.scrolltop(chat[0].scrollheight) // console.log(data); }); }); </script> </html>
package.json
{ "name": "livechat", "version": "1.0.0", "description": "realtime private chat server", "main": "app.js", "private": "true", "author": "", "license": "isc", "dependencies":{ "socket.io":"1.3.5", "mysql":"2.8.0", "express":"4.13.1", "express-session":"1.11.3" } }
settings.json
{ "port":"3000", "hosts":[ "127.0.0.1", "example.com" ], "secret":"faskjhfuwjhfjas" }
when trying read session anywhere in socket, 'undefined'.
you using new implementation of express-session , socket.io, 1 marries 2 well! https://www.npmjs.com/package/express-socket.io-session
package.json
add dependencies
"socket.io" : "*", "express-session" : "*", "express-socket.io-session" : "*"
app.js
add additional coding session data
var server = require("http").createserver(app), io = require("socket.io")(server), session = require("express-session")({ secret: "my-secret", resave: true, saveuninitialized: true }), sharedsession = require("express-socket.io-session"); // attach session app.use(session); // share session io sockets io.use(sharedsession(session)); io.on("connection", function(socket) { // accept login event user's data console.log(session.id); socket.on("login", function(userdata) { socket.handshake.session.userdata = userdata; }); socket.on("logout", function(userdata) { if (socket.handshake.session.userdata) { delete socket.handshake.session.userdata; } }); });
Comments
Post a Comment