fafhrd91 / pyramid_sockjs

44 stars 13 forks source link

Saving a variable to the session #14

Closed thapar closed 12 years ago

thapar commented 12 years ago

Within def on_open(self): of ChatSession(Session), If I do self.request.session['nick'] = Guest9324, and the same user opens a second browser tab to my webpage, the self.request.session dictionary is again empty. How can I overcome this?

thapar commented 12 years ago

Is there a way to access Pyramid's session? Or can pyramid_sockjs use cookies, so multiple tabs open to my webpage by a single user are recognized as the same user?

thapar commented 12 years ago
# -*- coding: utf-8 -*-
import sys
from pyramid_sockjs.session import Session

class ChatSession(Session):

    def check_cookie(self):
        ## I expected self.request.session to access Pyramid's native Session
        ## so I could check (or even create) a cookie
        return self.request.session

    def on_open(self):
        cookie = self.check_cookie()
        if cookie.get('nick'):
            current_session_list = json.loads(cookie['session_list'])
            current_session_list.append(self.id)
            cookie['session_list'] = json.dumps(current_session_list)

        else:
            ## however, the following assignment is not persistent
            ## because self.request.session is empty during
            ## any subsequent requests/msgs to the server by this user
            from random import randint
            self.request.session['nick'] = 'defaultnick%i' % randint(1000, 9999)
            self.request.session['session_list'] = json.dumps([self.id])

        self.manager.broadcast("Someone joined.")

    def on_message(self, message):
        self.manager.broadcast(message)

    def on_close(self):
        self.manager.broadcast("Someone left.")

if __name__ == '__main__':
    """ Simple sockjs chat """
    from pyramid.config import Configurator
    from pyramid_sockjs.paster import gevent_server_runner

    config = Configurator()
    config.include('pyramid_sockjs')

    config.add_sockjs_route(prefix='/__sockjs__', session=ChatSession)

    config.add_route('root', '/')
    config.add_view(route_name='root', renderer='__main__:chat.pt')

    app = config.make_wsgi_app()

    if len(sys.argv) > 1 and (sys.argv[1] == '-g'):
        from gunicorn.app.pasterapp import paste_server
        paste_server(app, port=8080, worker_class='gevent', workers=1)
    else:
        gevent_server_runner(app, {}, host='127.0.0.1')

My question is provided in the commented lines within the code above. Essentially, the followings steps can reproduce the issue:

1) Open a browser page and direct it to http://127.0.0.1:8080 2) Click Connect to open the sockjs connection (user should be assigned a defaultnick with some numbers, which should automatically be saved to the persistent session/cookie) 3) Open a new tab in the browser and direct it to http://127.0.0.1:8080 4) Click Connect and notice the server assigned a new defaultnick with different random numbers because the self.request.session was empty when accessing from the second browser tab

I would like to place a cookie on the end user's computer, so two tabs to my webpage can both be recognized as the same user. Therefore, I would like to access Pyramid's cookie/session, unless there is an internal pyramid_sockjs way to resolve this issue.

fafhrd91 commented 12 years ago

Basically, session should work. Probably you need to do something like 'request.session.save()' I'm not sure about save, but probably pyramid session machinery store values at the end of request, but pyramid_sockjs doesn't not releas request