Moved from Marshal to JSON session files.

This commit is contained in:
Martin Edenhofer 2015-01-13 15:53:15 +01:00
parent 4a9a2d8d10
commit 71d8af7a52
2 changed files with 24 additions and 5 deletions

View file

@ -59,7 +59,7 @@ class LongPollingController < ApplicationController
user_id = session[:user_id] user_id = session[:user_id]
user = {} user = {}
if user_id if user_id
user = User.find( user_id ) user = User.find( user_id ).attributes
end end
log 'notice', "send auth login (user_id #{user_id})", client_id log 'notice', "send auth login (user_id #{user_id})", client_id
Sessions.create( client_id, user, { :type => 'ajax' } ) Sessions.create( client_id, user, { :type => 'ajax' } )

View file

@ -37,7 +37,7 @@ returns
:user => session, :user => session,
:meta => meta, :meta => meta,
} }
file.write Marshal.dump(data) file.write data.to_json
} }
# send update to browser # send update to browser
@ -195,7 +195,7 @@ returns
path = @path + '/' + client_id.to_s path = @path + '/' + client_id.to_s
data[:meta][:last_ping] = Time.new.to_i.to_s data[:meta][:last_ping] = Time.new.to_i.to_s
File.open( path + '/session', 'wb' ) { |file| File.open( path + '/session', 'wb' ) { |file|
file.write Marshal.dump(data) file.write data.to_json
} }
true true
end end
@ -234,7 +234,11 @@ returns
file.flock( File::LOCK_EX ) file.flock( File::LOCK_EX )
all = file.read all = file.read
file.flock( File::LOCK_UN ) file.flock( File::LOCK_UN )
data = Marshal.load( all ) dataJSON = JSON.parse( all )
if dataJSON
data = self.symbolize_keys(dataJSON)
data[:user] = dataJSON['user'] # for compat. reasons
end
} }
rescue Exception => e rescue Exception => e
puts e.inspect puts e.inspect
@ -567,4 +571,19 @@ returns
puts "/LOOP #{client_id} - #{try_count}" puts "/LOOP #{client_id} - #{try_count}"
end end
def self.symbolize_keys(hash)
hash.inject({}){|result, (key, value)|
new_key = case key
when String then key.to_sym
else key
end
new_value = case value
when Hash then symbolize_keys(value)
else value
end
result[new_key] = new_value
result
}
end
end end