Added customer reconnect possibility on server side.

This commit is contained in:
Martin Edenhofer 2015-11-12 10:39:14 +01:00
parent edbbce1f50
commit d9a6c835b0
5 changed files with 105 additions and 72 deletions

View file

@ -4,9 +4,17 @@ class Chat < ApplicationModel
has_many :chat_topics
validates :name, presence: true
def state
def customer_state(session_id = nil)
return { state: 'chat_disabled' } if !Setting.get('chat')
if session_id
session = Chat.session_state(session_id)
return {
state: 'reconnect',
session: session,
}
end
if Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count > 0
if active_chat_count >= max_queue
return {
@ -21,6 +29,15 @@ class Chat < ApplicationModel
{ state: 'offline' }
end
def self.session_state(session_id)
session_attributes = []
chat_session_id = Chat::Session.find_by(session_id: session_id)
Chat::Message.where(chat_session_id: chat_session_id.id).each { |message|
session_attributes.push message.attributes
}
session_attributes
end
def self.agent_state(user_id)
return { state: 'chat_disabled' } if !Setting.get('chat')
actice_sessions = []

View file

@ -13,9 +13,14 @@ class Sessions::Event::ChatStatusCustomer < Sessions::Event::ChatBase
}
end
# check if it's a chat sessin reconnect
session_id = nil
if @data['data']['session_id']
session_id = @data['data']['session_id']
end
{
event: 'chat_status_customer',
data: chat.state,
data: chat.customer_state(session_id),
}
end
end

View file

@ -129,6 +129,9 @@ do($ = window.jQuery, window) ->
@log 'debug', 'Zammad Chat: Chat is disabled'
when 'no_seats_available'
@log 'debug', 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue
when 'reconnect'
# old messages
@log 'debug', 'old messages', pipe.data.session
onReady: =>
if @options.show
@ -153,7 +156,8 @@ do($ = window.jQuery, window) ->
# send typing start event
if !@isTyping
@isTyping = true
@send 'chat_session_typing', {session_id: @session_id}
@send 'chat_session_typing',
session_id: @session_id
onTypingEnd: =>
@isTyping = false
@ -235,7 +239,8 @@ do($ = window.jQuery, window) ->
@disconnect()
@isOpen = false
@send 'chat_session_close', {session_id: @session_id}
@send 'chat_session_close',
session_id: @session_id
hide: ->
@el.removeClass('zammad-chat-is-visible')
@ -319,7 +324,8 @@ do($ = window.jQuery, window) ->
@ws = new window.WebSocket(@host)
@ws.onopen = =>
@log 'debug', 'ws connected'
@send 'chat_status_customer'
@send 'chat_status_customer',
session_id: @session_id
@setAgentOnlineState(true)
@ws.onmessage = @onWebSocketMessage

View file

@ -1,68 +1,3 @@
if (!window.zammadChatTemplates) {
window.zammadChatTemplates = {};
}
window.zammadChatTemplates["agent"] = function (__obj) {
if (!__obj) __obj = {};
var __out = [], __capture = function(callback) {
var out = __out, result;
__out = [];
callback.call(this);
result = __out.join('');
__out = out;
return __safe(result);
}, __sanitize = function(value) {
if (value && value.ecoSafe) {
return value;
} else if (typeof value !== 'undefined' && value != null) {
return __escape(value);
} else {
return '';
}
}, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
__safe = __obj.safe = function(value) {
if (value && value.ecoSafe) {
return value;
} else {
if (!(typeof value !== 'undefined' && value != null)) value = '';
var result = new String(value);
result.ecoSafe = true;
return result;
}
};
if (!__escape) {
__escape = __obj.escape = function(value) {
return ('' + value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
}
(function() {
(function() {
if (this.agent.avatar) {
__out.push('\n<img class="zammad-chat-agent-avatar" src="');
__out.push(__sanitize(this.agent.avatar));
__out.push('">\n');
}
__out.push('\n<span class="zammad-chat-agent-sentence">\n <span class="zammad-chat-agent-name">');
__out.push(__sanitize(this.agent.name));
__out.push('</span> ');
__out.push(this.agentPhrase);
__out.push('\n</span>');
}).call(this);
}).call(__obj);
__obj.safe = __objSafe, __obj.escape = __escape;
return __out.join('');
};
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
slice = [].slice;
@ -267,6 +202,9 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
break;
case 'no_seats_available':
this.log('debug', 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue);
break;
case 'reconnect':
this.log('debug', 'old messages', pipe.data.session);
}
}
}
@ -479,7 +417,9 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
this.ws.onopen = (function(_this) {
return function() {
_this.log('debug', 'ws connected');
_this.send('chat_status_customer');
_this.send('chat_status_customer', {
session_id: _this.session_id
});
return _this.setAgentOnlineState(true);
};
})(this);
@ -559,6 +499,71 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
});
})(window.jQuery, window);
if (!window.zammadChatTemplates) {
window.zammadChatTemplates = {};
}
window.zammadChatTemplates["agent"] = function (__obj) {
if (!__obj) __obj = {};
var __out = [], __capture = function(callback) {
var out = __out, result;
__out = [];
callback.call(this);
result = __out.join('');
__out = out;
return __safe(result);
}, __sanitize = function(value) {
if (value && value.ecoSafe) {
return value;
} else if (typeof value !== 'undefined' && value != null) {
return __escape(value);
} else {
return '';
}
}, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
__safe = __obj.safe = function(value) {
if (value && value.ecoSafe) {
return value;
} else {
if (!(typeof value !== 'undefined' && value != null)) value = '';
var result = new String(value);
result.ecoSafe = true;
return result;
}
};
if (!__escape) {
__escape = __obj.escape = function(value) {
return ('' + value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
}
(function() {
(function() {
if (this.agent.avatar) {
__out.push('\n<img class="zammad-chat-agent-avatar" src="');
__out.push(__sanitize(this.agent.avatar));
__out.push('">\n');
}
__out.push('\n<span class="zammad-chat-agent-sentence">\n <span class="zammad-chat-agent-name">');
__out.push(__sanitize(this.agent.name));
__out.push('</span> ');
__out.push(this.agentPhrase);
__out.push('\n</span>');
}).call(this);
}).call(__obj);
__obj.safe = __objSafe, __obj.escape = __escape;
return __out.join('');
};
/*!
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):

File diff suppressed because one or more lines are too long