Is there anyway to save data for each connected client? I would like to have a hash of state data I can save for each connection... I figured since we are statefull here we dont need to replicate something like http sessions... Anyone got any suggestions?
On 12/27/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote:> Is there anyway to save data for each connected client? > > I would like to have a hash of state data I can save for each connection... > > I figured since we are statefull here we dont need to replicate > something like http sessions... > > Anyone got any suggestions? > _______________________________________________Sure. If you use the following idiom when writing your connection handler, you can add data to each connection or store data about them somewhere else: class MyHandler < EventMachine::Connection def initialize *args super end end EventMachine.run { EventMachine.start_server( ip_address, ip_port, MyHandler) {|new_connection| # do whatever you want with new_connection, which is an instance of MyHandler. } }
Francis Cianfrocca wrote:> On 12/27/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote: > >> Is there anyway to save data for each connected client? >> >> I would like to have a hash of state data I can save for each connection... >> >> I figured since we are statefull here we dont need to replicate >> something like http sessions... >> >> Anyone got any suggestions? >> _______________________________________________ >> > > > Sure. If you use the following idiom when writing your connection > handler, you can add data to each connection or store data about them > somewhere else: > > class MyHandler < EventMachine::Connection > def initialize *args > super > end > end > > > EventMachine.run { > EventMachine.start_server( ip_address, ip_port, MyHandler) {|new_connection| > # do whatever you want with new_connection, which is an instance > of MyHandler. > } > } > >But we warned, ruby interpretor won''t love you for storing all this growing data. You can use a in memory database like purple for offloading the data storage to an efficient engine.
You mean like this ? class Server < EventMachine::Connection def initialize *args super end def receive_data(input) # end end EventMachine.run { EventMachine.start_server( "127.0.0.1", 8080, Server){|new_connection| # do whatever you want with new_connection } } Francis Cianfrocca wrote:> On 12/27/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote: >> Is there anyway to save data for each connected client? >> >> I would like to have a hash of state data I can save for each connection... >> >> I figured since we are statefull here we dont need to replicate >> something like http sessions... >> >> Anyone got any suggestions? >> _______________________________________________ > > > Sure. If you use the following idiom when writing your connection > handler, you can add data to each connection or store data about them > somewhere else: > > class MyHandler < EventMachine::Connection > def initialize *args > super > end > end > > > EventMachine.run { > EventMachine.start_server( ip_address, ip_port, MyHandler) {|new_connection| > # do whatever you want with new_connection, which is an instance > of MyHandler. > } > } > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
Why do you tell me to code within the block given to start_server? Why wouldn''t I just create a @session hash in the initialize method of MyHandler? Francis Cianfrocca wrote:> On 12/27/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote: >> Is there anyway to save data for each connected client? >> >> I would like to have a hash of state data I can save for each connection... >> >> I figured since we are statefull here we dont need to replicate >> something like http sessions... >> >> Anyone got any suggestions? >> _______________________________________________ > > > Sure. If you use the following idiom when writing your connection > handler, you can add data to each connection or store data about them > somewhere else: > > class MyHandler < EventMachine::Connection > def initialize *args > super > end > end > > > EventMachine.run { > EventMachine.start_server( ip_address, ip_port, MyHandler) {|new_connection| > # do whatever you want with new_connection, which is an instance > of MyHandler. > } > } > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
On 12/29/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote:> Why do you tell me to code within the block given to start_server? > > Why wouldn''t I just create a @session hash in the initialize method of > MyHandler? >You can do it either way. The point of doing it in a block of course is that it''s a closure, so you have access to other local variables that are in context with the block, etc.
I''m currently testing the approach below but wanted to see what you guys think of it... module ServerMixin # We have a new connection def post_init puts "Received a new connection @ #{Time.now}" @session = {} end def receive_data data end end Francis Cianfrocca wrote:> On 12/29/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote: >> Why do you tell me to code within the block given to start_server? >> >> Why wouldn''t I just create a @session hash in the initialize method of >> MyHandler? >> > > > You can do it either way. The point of doing it in a block of course > is that it''s a closure, so you have access to other local variables > that are in context with the block, etc. > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
On 12/29/06, Daniel Aquino <mr.danielaquino at gmail.com> wrote:> I''m currently testing the approach below but wanted to see what you guys > think of it... > > module ServerMixin > # We have a new connection > def post_init > puts "Received a new connection @ #{Time.now}" > @session = {} > end > def receive_data data > end > end >That''s fine if it does what you want. I always have a little trouble with modules and classes and instance variables. Your module gets mixed into an instance of class EventMachine::Connection, so keep that in mind if you have trouble.