Re: [Cppcms-users] SSE keep-alive
Brought to you by:
artyom-beilis
|
From: Artyom B. <art...@ya...> - 2013-04-02 20:15:01
|
Small notes: 1. Session is saved when the output stream is accessed first time, as you need to provide set session cookies. Remember the session data may be stored entirely on the client side in signed cookies. Saving session after output was provided is similar to an attempt to set a cookie **after** HTTP headers were sent. 2. Calling changing session values and calling save after the output was generated has undefined behavior. So your code is incorrect. I think you mix up two different things: HTTP Stream session - the state where the connection is open and you push the data to the stream (it is represented by cppcms::http::context and actual open TCP/IP socket) and the CppCMS/Web session the information shared **accorss** different connections - that is based cookies - the state for stateless HTTP. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ ----- Original Message ----- > From: Christian Gmeiner <chr...@gm...> > To: Artyom Beilis <art...@ya...>; cpp...@li... > Cc: > Sent: Tuesday, April 2, 2013 7:58 PM > Subject: Re: [Cppcms-users] SSE keep-alive > > 2013/4/2 Christian Gmeiner <chr...@gm...>: >> 2013/4/2 Artyom Beilis <art...@ya...>: >>> Your code is incorrect, you must not use Mutexes... >>> >>> Instead of this: >>> >>>> void push(std::string const &event,std::string const > &data,bool >>>> send=true) >>>> { >>>> message msg; >>>> msg.event = event; >>>> msg.data = data; >>>> >>>> mutex.lock(); >>>> messages_.push(msg); >>>> mutex.unlock(); >>>> >>>> if(send) >>>> broadcast(); >>>> } >>> >>> >>> That I assume is called form an external thread you need to do > something different >>> >>> >>> void thread_safe_push(std::string const &event,std::string const > &data) >>> { message msg; >>> msg.event = event; >>> msg.data = data; >>> >>> >>> service().post([=] { (lambda expression) >>> >>> // EXECUTED IN THE EVENT LOOP THREAD!!! >>> messages_.push(msg); >>> broadcast(); >>> >>> }); >>> >>> } >>> >>> >>> or without C++11 >>> >>> void thread_safe_push(std::string const &event,std::string const > &data) >>> { message msg; >>> msg.event = event; >>> msg.data = data; >>> >>> >>> > service().post(boost::bind(&event_fifo::thread_unsef_push,this,msg)); >>> >>> } >>> >>> >>> void thread_unsefe_push(message const &msg) >>> >>> { messages_.push(msg); >>> broadcast(); >>> >>> } >>> >> >> Thanks... I got rid of the mutex. Is there a way to renew a session? I >> have tried to load() and save() the session during keep alive of the >> event stream, but it does not work as expected. >> >> void event_source::keep_alive(char const *comment) >> { >> if(closing_) { >> return; >> } >> for(streamers_type::iterator >> it=streamers_.begin();it!=streamers_.end();) { >> booster::intrusive_ptr<details::post_send> ps = *it; >> streamers_type::iterator tmp = it++; >> streamers_.erase(tmp); >> >> ps->stream().context()->response().out() << > ':' << >> comment << "\n\n"; >> ps->stream().context()->async_flush_output(ps); >> >> ps->stream().context()->session().load(); >> ps->stream().context()->session().save(); >> } >> >> ... >> >> >> My current session timeout is set to 10 minutes. If a script gets >> executed the GUI in the browser >> shows a modal dialog with the output of the executed process - send via > SSE. >> >> If the script runs longer then session timeout and gc() gets called >> the session will be removed. I want >> to renew the session in the keep_alive action of the SSE stream. >> > > As far as I can tell the session can only be loaded and saves once per > http_context. So > I added the following method: > > > void session_interface::renew_session() > { > loaded_ = 0; > saved_ = 0; > > load(); > save(); > } > > > And now I can renew the session. > > thanks > -- > Christian Gmeiner, MSc > |