2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-12-19 14:31:06 +00:00
|
|
|
class WebsocketServer
|
|
|
|
|
|
|
|
cattr_reader :clients, :options
|
|
|
|
|
|
|
|
def self.run(options)
|
|
|
|
@options = options
|
|
|
|
@clients = {}
|
|
|
|
|
|
|
|
Rails.configuration.interface = 'websocket'
|
|
|
|
EventMachine.run do
|
2021-07-16 13:38:01 +00:00
|
|
|
EventMachine::WebSocket.start(host: @options[:b], port: @options[:p], secure: @options[:s], tls_options: @options[:tls_options]) do |ws|
|
2018-12-19 14:31:06 +00:00
|
|
|
|
|
|
|
# register client connection
|
|
|
|
ws.onopen do |handshake|
|
|
|
|
WebsocketServer.onopen(ws, handshake)
|
|
|
|
end
|
|
|
|
|
|
|
|
# unregister client connection
|
|
|
|
ws.onclose do
|
|
|
|
WebsocketServer.onclose(ws)
|
|
|
|
end
|
|
|
|
|
|
|
|
# manage messages
|
|
|
|
ws.onmessage do |msg|
|
|
|
|
WebsocketServer.onmessage(ws, msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# check unused connections
|
|
|
|
EventMachine.add_timer(0.5) do
|
|
|
|
WebsocketServer.check_unused_connections
|
|
|
|
end
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# check open unused connections, kick all connection without activity in the last 2 minutes
|
2018-12-19 14:31:06 +00:00
|
|
|
EventMachine.add_periodic_timer(120) do
|
|
|
|
WebsocketServer.check_unused_connections
|
|
|
|
end
|
|
|
|
|
|
|
|
EventMachine.add_periodic_timer(20) do
|
|
|
|
WebsocketServer.log_status
|
|
|
|
end
|
|
|
|
|
|
|
|
EventMachine.add_periodic_timer(0.4) do
|
|
|
|
WebsocketServer.send_to_client
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.onopen(websocket, handshake)
|
|
|
|
headers = handshake.headers
|
|
|
|
client_id = websocket.object_id.to_s
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'Client connected.', client_id
|
2021-07-16 13:38:01 +00:00
|
|
|
Sessions.create(client_id, {}, { type: 'websocket' })
|
2018-12-19 14:31:06 +00:00
|
|
|
|
|
|
|
return if @clients.include? client_id
|
|
|
|
|
|
|
|
@clients[client_id] = {
|
|
|
|
websocket: websocket,
|
|
|
|
last_ping: Time.now.utc.to_i,
|
|
|
|
error_count: 0,
|
|
|
|
headers: headers,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.onclose(websocket)
|
|
|
|
client_id = websocket.object_id.to_s
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'Client disconnected.', client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
|
|
|
|
# removed from current client list
|
|
|
|
if @clients.include? client_id
|
|
|
|
@clients.delete client_id
|
|
|
|
end
|
|
|
|
|
|
|
|
Sessions.destroy(client_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.onmessage(websocket, msg)
|
|
|
|
client_id = websocket.object_id.to_s
|
|
|
|
log 'debug', "received: #{msg} ", client_id
|
|
|
|
begin
|
|
|
|
data = JSON.parse(msg)
|
|
|
|
rescue => e
|
|
|
|
log 'error', "can't parse message: #{msg}, #{e.inspect}", client_id
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if connection not already exists
|
|
|
|
return if !@clients[client_id]
|
|
|
|
|
|
|
|
Sessions.touch(client_id) # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
@clients[client_id][:last_ping] = Time.now.utc.to_i
|
|
|
|
|
|
|
|
# spool messages for new connects
|
|
|
|
if data['spool']
|
|
|
|
Sessions.spool_create(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
if data['event']
|
|
|
|
log 'debug', "execute event '#{data['event']}'", client_id
|
|
|
|
message = Sessions::Event.run(
|
2018-12-20 08:16:35 +00:00
|
|
|
event: data['event'],
|
|
|
|
payload: data,
|
|
|
|
session: @clients[client_id][:session],
|
2020-02-13 08:27:36 +00:00
|
|
|
headers: @clients[client_id][:headers],
|
2018-12-19 14:31:06 +00:00
|
|
|
client_id: client_id,
|
2018-12-20 08:16:35 +00:00
|
|
|
clients: @clients,
|
|
|
|
options: @options,
|
2018-12-19 14:31:06 +00:00
|
|
|
)
|
|
|
|
if message
|
|
|
|
websocket_send(client_id, message)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
log 'error', "unknown message '#{data.inspect}'", client_id
|
|
|
|
end
|
2021-08-25 12:24:42 +00:00
|
|
|
ensure
|
|
|
|
ActiveSupport::CurrentAttributes.clear_all
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.websocket_send(client_id, data)
|
2020-11-05 16:31:00 +00:00
|
|
|
msg = if data.instance_of?(Array)
|
2018-12-19 14:31:06 +00:00
|
|
|
data.to_json
|
2020-11-05 16:31:00 +00:00
|
|
|
else
|
|
|
|
"[#{data.to_json}]"
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
log 'debug', "send #{msg}", client_id
|
|
|
|
if !@clients[client_id]
|
|
|
|
log 'error', "no such @clients for #{client_id}", client_id
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@clients[client_id][:websocket].send(msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.check_unused_connections
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'check unused idle connections...'
|
2018-12-19 14:31:06 +00:00
|
|
|
|
|
|
|
idle_time_in_sec = 4 * 60
|
|
|
|
|
|
|
|
# close unused web socket sessions
|
|
|
|
@clients.each do |client_id, client|
|
|
|
|
|
2021-07-16 13:38:01 +00:00
|
|
|
next if (client[:last_ping].to_i + idle_time_in_sec) >= Time.now.utc.to_i
|
2018-12-19 14:31:06 +00:00
|
|
|
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'closing idle websocket connection', client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
|
|
|
|
# remember to not use this connection anymore
|
|
|
|
client[:disconnect] = true
|
|
|
|
|
|
|
|
# try to close regular
|
|
|
|
client[:websocket].close_websocket
|
|
|
|
|
|
|
|
# delete session from client list
|
|
|
|
sleep 0.3
|
|
|
|
@clients.delete(client_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# close unused ajax long polling sessions
|
|
|
|
clients = Sessions.destroy_idle_sessions(idle_time_in_sec)
|
|
|
|
clients.each do |client_id|
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'closing idle long polling connection', client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_to_client
|
|
|
|
return if @clients.size.zero?
|
|
|
|
|
2021-07-16 13:44:10 +00:00
|
|
|
# log 'debug', 'checking for data to send...'
|
2018-12-19 14:31:06 +00:00
|
|
|
@clients.each do |client_id, client|
|
|
|
|
next if client[:disconnect]
|
|
|
|
|
|
|
|
log 'debug', 'checking for data...', client_id
|
|
|
|
begin
|
|
|
|
queue = Sessions.queue(client_id)
|
|
|
|
next if queue.blank?
|
|
|
|
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'send data to client', client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
websocket_send(client_id, queue)
|
|
|
|
rescue => e
|
2020-09-30 09:07:01 +00:00
|
|
|
log 'error', "problem:#{e.inspect}", client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
|
|
|
|
# disconnect client
|
|
|
|
client[:error_count] += 1
|
2020-09-30 09:07:01 +00:00
|
|
|
if client[:error_count] > 20 && @clients.include?(client_id)
|
|
|
|
@clients.delete client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.log_status
|
|
|
|
# websocket
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', "Status: websocket clients: #{@clients.size}"
|
2018-12-19 14:31:06 +00:00
|
|
|
@clients.each_key do |client_id|
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'working...', client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# ajax
|
|
|
|
client_list = Sessions.list
|
|
|
|
clients = 0
|
|
|
|
client_list.each_value do |client|
|
|
|
|
next if client[:meta][:type] == 'websocket'
|
|
|
|
|
2021-07-16 14:10:31 +00:00
|
|
|
clients += 1
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', "Status: ajax clients: #{clients}"
|
2018-12-19 14:31:06 +00:00
|
|
|
client_list.each do |client_id, client|
|
|
|
|
next if client[:meta][:type] == 'websocket'
|
|
|
|
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'working...', client_id
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.log(level, data, client_id = '-')
|
2020-09-30 09:07:01 +00:00
|
|
|
return if !@options[:v] && level == 'debug'
|
|
|
|
|
2018-12-19 14:31:06 +00:00
|
|
|
puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}" # rubocop:disable Rails/Output
|
2021-07-16 13:44:10 +00:00
|
|
|
# puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
|
2018-12-19 14:31:06 +00:00
|
|
|
end
|
|
|
|
end
|