Martin Edenhofer 9 лет назад
Родитель
Сommit
c8ae1d38e1
2 измененных файлов с 19 добавлено и 17 удалено
  1. 12 10
      lib/sessions.rb
  2. 7 7
      script/websocket-server.rb

+ 12 - 10
lib/sessions.rb

@@ -1,3 +1,5 @@
+# rubocop:disable Rails/TimeZone
+
 require 'json'
 require 'session_helper'
 
@@ -33,7 +35,7 @@ returns
     session_file = "#{path_tmp}/session"
 
     # collect session data
-    meta[:last_ping] = Time.zone.now.to_i.to_s
+    meta[:last_ping] = Time.now.utc.to_i.to_s
     data = {
       user: session,
       meta: meta,
@@ -189,7 +191,7 @@ returns
     list_of_closed_sessions = []
     clients                 = Sessions.list
     clients.each { |client_id, client|
-      if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.zone.now.to_i
+      if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.now.utc.to_i
         list_of_closed_sessions.push client_id
         Sessions.destory( client_id )
       end
@@ -213,7 +215,7 @@ returns
     data = get(client_id)
     return false if !data
     path = "#{@path}/#{client_id}"
-    data[:meta][:last_ping] = Time.zone.now.to_i.to_s
+    data[:meta][:last_ping] = Time.now.utc.to_i.to_s
     content = data.to_json
     File.open( path + '/session', 'wb' ) { |file|
       file.write content
@@ -289,7 +291,7 @@ returns
 
   def self.send( client_id, data )
     path     = "#{@path}/#{client_id}/"
-    filename = "send-#{ Time.zone.now.to_f }"
+    filename = "send-#{ Time.now.utc.to_f }"
     check    = true
     count    = 0
     while check
@@ -422,11 +424,11 @@ returns
   def self.spool_create( msg )
     path = "#{@path}/spool/"
     FileUtils.mkpath path
-    file_path = path + "/#{Time.zone.now.to_f}-#{rand(99_999)}"
+    file_path = path + "/#{Time.now.utc.to_f}-#{rand(99_999)}"
     File.open( file_path, 'wb' ) { |file|
       data = {
         msg: msg,
-        timestamp: Time.zone.now.to_i,
+        timestamp: Time.now.utc.to_i,
       }
       file.write data.to_json
     }
@@ -457,7 +459,7 @@ returns
         end
 
         # ignore message older then 48h
-        if spool['timestamp'] + (2 * 86_400) < Time.zone.now.to_i
+        if spool['timestamp'] + (2 * 86_400) < Time.now.utc.to_i
           to_delete.push "#{path}/#{entry}"
           next
         end
@@ -568,7 +570,7 @@ returns
 
 =end
 
-  def self.thread_client(client_id, try_count = 0, try_run_time = Time.zone.now)
+  def self.thread_client(client_id, try_count = 0, try_run_time = Time.now.utc)
     Rails.logger.debug "LOOP #{client_id} - #{try_count}"
     begin
       Sessions::Client.new(client_id)
@@ -586,10 +588,10 @@ returns
       try_count += 1
 
       # reset error counter if to old
-      if try_run_time + ( 60 * 5 ) < Time.zone.now
+      if try_run_time + ( 60 * 5 ) < Time.now.utc
         try_count = 0
       end
-      try_run_time = Time.zone.now
+      try_run_time = Time.now.utc
 
       # restart job again
       if try_run_max > try_count

+ 7 - 7
script/websocket-server.rb

@@ -96,7 +96,7 @@ EventMachine.run {
       if !@clients.include? client_id
         @clients[client_id] = {
           websocket: ws,
-          last_ping: Time.now,
+          last_ping: Time.now.utc.iso8601,
           error_count: 0,
         }
       end
@@ -140,7 +140,7 @@ EventMachine.run {
 
         # error handling
         if data['timestamp']
-          log 'notice', "request spool data > '#{Time.at(data['timestamp'])}'", client_id
+          log 'notice', "request spool data > '#{Time.at(data['timestamp']).utc.iso8601}'", client_id
         else
           log 'notice', 'request spool with init data', client_id
         end
@@ -165,7 +165,7 @@ EventMachine.run {
 
         # send spool:sent event to client
         log 'notice', 'send spool:sent event', client_id
-        @clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.to_i.to_s + '}}]' )
+        @clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.utc.iso8601.to_i.to_s + '}}]' )
       end
 
       # get session
@@ -176,7 +176,7 @@ EventMachine.run {
         # remember ping, send pong back
       elsif data['action'] == 'ping'
         Sessions.touch(client_id)
-        @clients[client_id][:last_ping] = Time.now
+        @clients[client_id][:last_ping] = Time.now.utc.iso8601
         @clients[client_id][:websocket].send( '[{"action":"pong"}]' )
 
         # broadcast
@@ -299,7 +299,7 @@ EventMachine.run {
     # close unused web socket sessions
     @clients.each { |client_id, client|
 
-      next if ( client[:last_ping] + idle_time_in_sec ) >= Time.now
+      next if ( client[:last_ping] + idle_time_in_sec ) >= Time.now.utc.iso8601
 
       log 'notice', 'closing idle websocket connection', client_id
 
@@ -325,8 +325,8 @@ EventMachine.run {
     if !@options[:v]
       return if level == 'debug'
     end
-    puts "#{Time.now}:client(#{ client_id }) #{ data }"
-    #    puts "#{Time.now}:#{ level }:client(#{ client_id }) #{ data }"
+    puts "#{Time.now.utc.iso8601}:client(#{ client_id }) #{ data }"
+    #    puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
   end
 
 }