trabajo-afectivo/app/controllers/long_polling_controller.rb

115 lines
2.7 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2012-11-26 23:32:55 +00:00
class LongPollingController < ApplicationController
2017-09-23 06:25:55 +00:00
skip_before_action :session_update # prevent race conditions
2012-11-26 23:32:55 +00:00
# GET /api/v1/message_send
2012-11-26 23:32:55 +00:00
def message_send
new_connection = false
# check client id
client_id = client_id_verify
2012-11-26 23:32:55 +00:00
if !client_id
new_connection = true
client_id = client_id_gen
2015-05-04 18:58:28 +00:00
log 'new client connection', client_id
2012-11-26 23:32:55 +00:00
end
if !params['data']
params['data'] = {}
end
2015-12-09 13:09:37 +00:00
session_data = {}
2017-11-23 08:09:44 +00:00
if current_user&.id
2015-12-09 13:09:37 +00:00
session_data = { 'id' => current_user.id }
end
2012-11-26 23:32:55 +00:00
# spool messages for new connects
if params['data']['spool']
2015-12-09 13:09:37 +00:00
Sessions.spool_create(params['data'])
end
2015-12-09 13:09:37 +00:00
if params['data']['event'] == 'login'
Sessions.create(client_id, session_data, { type: 'ajax' })
elsif params['data']['event']
message = Sessions::Event.run(
event: params['data']['event'],
payload: params['data'],
session: session_data,
client_id: client_id,
clients: {},
options: {},
)
if message
Sessions.send(client_id, message)
end
2015-12-09 13:09:37 +00:00
else
log "unknown message '#{params['data'].inspect}'", client_id
2012-11-26 23:32:55 +00:00
end
if new_connection
result = { client_id: client_id }
render json: result
2012-11-26 23:32:55 +00:00
else
render json: {}
2012-11-26 23:32:55 +00:00
end
end
# GET /api/v1/message_receive
2012-11-26 23:32:55 +00:00
def message_receive
# check client id
client_id = client_id_verify
raise Exceptions::UnprocessableEntity, 'Invalid client_id receive!' if !client_id
2012-11-26 23:32:55 +00:00
# check queue to send
2012-11-26 23:32:55 +00:00
begin
# update last ping
4.times do
sleep 0.25
end
#sleep 1
2017-11-23 08:09:44 +00:00
Sessions.touch(client_id) # rubocop:disable Rails/SkipsModelValidations
# set max loop time to 24 sec. because of 30 sec. timeout of mod_proxy
2015-12-09 13:09:37 +00:00
count = 3
if Rails.env.production?
count = 12
end
loop do
2012-11-26 23:32:55 +00:00
count = count - 1
2015-12-09 13:09:37 +00:00
queue = Sessions.queue(client_id)
2012-11-26 23:32:55 +00:00
if queue && queue[0]
2015-05-04 19:11:20 +00:00
logger.debug "send #{queue.inspect} to #{client_id}"
render json: queue
2012-11-26 23:32:55 +00:00
return
end
8.times do
sleep 0.25
end
#sleep 2
if count.zero?
2015-12-09 13:09:37 +00:00
render json: { event: 'pong' }
2012-11-26 23:32:55 +00:00
return
end
end
rescue => e
raise Exceptions::UnprocessableEntity, 'Invalid client_id in receive loop!'
2012-11-26 23:32:55 +00:00
end
end
private
def client_id_gen
rand(9_999_999_999).to_s
end
def client_id_verify
return if !params[:client_id]
sessions = Sessions.sessions
2015-12-09 13:09:37 +00:00
return if !sessions.include?(params[:client_id].to_s)
2015-04-27 20:49:17 +00:00
params[:client_id].to_s
end
2015-05-04 18:58:28 +00:00
def log( data, client_id = '-' )
logger.info "client(#{client_id}) #{data}"
end
end