You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

156 lines
6.4 KiB

<script>
var socket;
function getOpenOrdersList() {
$.ajax({
url: '/api/orders?status=created',
type: 'get',
success: function (data) {
$.each(data.results, function (key, value) {
open_order_list.push(parseInt(value.id))
})
}
})
}
function message_count_plus() {
console.log('Message +1');
var all_messages = $('.js-all-messages');
var count = parseInt(all_messages.html()) + 1;
if (count > 99) count = '99+';
all_messages.html(count);
}
var wsConnect;
function WSConnection() {
'use strict';
this.socket = {};
}
WSConnection.prototype.connect = function (url) {
'use strict';
return new Promise((resolve, reject) => {
var self = this;
this.socket = new WebSocket(url);
// Пользователь на странице Чата(Переговорная)
var inChatPage = true;
this.socket.onopen = function () {
print.ws_print('Socket open');
resolve(this);
try {
connect();
} catch (ReferenceError) {
print.ws_print('No additional WS connect');
inChatPage = false;
}
};
this.socket.onmessage = function (event) {
print.ws_print("New message: " + JSON.parse(event.data));
var notificationData = JSON.parse(event.data);
{# if (open_order_list.indexOf(parseInt(notificationData.order_id)) != -1) {#}
{# message_count_plus('#my_office');#}
{# }#}
{# message_count_plus('.js-all-messages');#}
var outMessage = "";
if (notificationData.answer_type == 'add_message_contact') {
outMessage += "<a href='/chat/?user_id=" + notificationData.sender_id + "'>" + notificationData.msg + "<a>";
} else if ((notificationData.answer_type == 'approve_stages') || (notificationData.answer_type == 'add_message_order')) {
outMessage += "<a href='/chat/#order" + notificationData.order_id + "'>" + notificationData.msg + "<a>";
}
if (!inChatPage) message_count_plus();
print.ws_print("Вам пришло новое сообщение!", outMessage);
if (userId != notificationData.sender_id) {
$.jGrowl("Вам пришло новое сообщение!<br />" + outMessage, {life: 15000});
}
};
this.socket.add_message = function (messageData) {
print.ws_print("add Message");
self.socket.send(JSON.stringify(messageData))
};
this.socket.onerror = function (error) {
print.ws_print('Error: ' + error);
reject(error);
};
this.socket.onclose = function (event) {
print.ws_print("Websocket closed: " + JSON.stringify(event));
// TODO: Сдеалть реконнект#}
setTimeout(function () {
console.log("reconnect to WS");
wsConnect = new WSConnection().connect(url);
}, 2000);
};
});
};
{# var SocketHandlerMain = function (userId) {#}
{# var domain = '{{ request.META.HTTP_HOST }}';#}
{# var port = '{{ request.META.SERVER_PORT }}';#}
{# var queryString = '{{ request.get_full_path }}';#}
{##}
{# if ((queryString.indexOf('/chat') != 0) && (queryString.indexOf('/users/contractor-office/work-projects') != 0)) {#}
{# domain = domain.replace(':' + port, ':8888');#}
{# if (window.location.protocol == 'https:') {#}
{# var ws = "wss://";#}
{# } else {#}
{# var ws = "ws://";#}
{# }#}
{##}
{# var url = ws + domain + '/chat/' + userId + '/';#}
{# var sock = new WebSocket(url);#}
{# sock.onopen = function () {#}
{# print.ws_print('Socket open');#}
{# };#}
{##}
{# sock.onmessage = function (event) {#}
{# var notificationData = JSON.parse(event.data);#}
{# if (open_order_list.indexOf(parseInt(notificationData.order_id)) != -1) {#}
{# message_count_plus('#my_office');#}
{# }#}
{# message_count_plus('.js-all-messages');#}
{# var outMessage = "";#}
{# if (notificationData.answer_type == 'add_message_contact') {#}
{# outMessage += "<a href='/chat/?user_id=" + notificationData.sender_id + "'>" + notificationData.msg + "<a>";#}
{# } else if ((notificationData.answer_type == 'approve_stages') || (notificationData.answer_type == 'add_message_order')) {#}
{# outMessage += "<a href='/chat/#order" + notificationData.order_id + "'>" + notificationData.msg + "<a>";#}
{# }#}
{# print.ws_print("Вам пришло новое сообщение!", outMessage);#}
{# $.jGrowl("Вам пришло новое сообщение!<br />" + outMessage, {life: 15000});#}
{# };#}
{##}
{# this.send_message = function (messageData) {#}
{# print.ws_print("send message", messageData);#}
{# sock.send(JSON.stringify(messageData));#}
{# };#}
{# }#}
{# };#}
var userId = '{{ request.user.pk }}';
if (userId) {
var domain = '{{ request.META.HTTP_HOST }}';
var port = '{{ request.META.SERVER_PORT }}';
domain = domain.replace(':' + port, ':8888');
if (window.location.protocol == 'https:') {
var ws = "wss://";
} else {
var ws = "ws://";
}
var url = ws + domain + '/chat/' + userId + '/';
$(function () {
console.log('Create WS connection');
wsConnect = new WSConnection().connect(url);
wsConnect.then(function (_socket) {
socket = _socket;
})
});
{# var open_order_list = [];#}
{# getOpenOrdersList();#}
}
</script>