Send messages using SockJS on staging

(imported from commit 812d20b1eab76eeb0d8fb92029fdb38d5faa9cce)
This commit is contained in:
Zev Benjamin
2013-09-11 14:42:12 -04:00
parent 89bcabbb65
commit ac87d92138
2 changed files with 44 additions and 23 deletions

View File

@@ -315,6 +315,36 @@ function compose_error(error_text, bad_input) {
var send_options; var send_options;
function send_message_ajax(request, success) {
$.ajax({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
url: '/json/send_message',
type: 'POST',
data: request,
success: success,
error: function (xhr, error_type) {
if (error_type !== 'timeout' && reload.is_pending()) {
// The error might be due to the server changing
reload.initiate({immediate: true, send_after_reload: true});
return;
}
var response = util.xhr_error_message("Error sending message", xhr);
compose_error(response, $('#new_message_content'));
}
});
}
var socket = new Socket("/sockjs");
function send_message_socket(request, success) {
socket.send(request, success, function (type, resp) {
var err_msg = "Error sending message";
if (type === 'response') {
err_msg += ": " + resp.msg;
}
compose_error(err_msg, $('#new_message_content'));
});
}
function send_message() { function send_message() {
var request = create_message_object(); var request = create_message_object();
exports.snapshot_message(request); exports.snapshot_message(request);
@@ -325,12 +355,7 @@ function send_message() {
request.to = JSON.stringify([request.to]); request.to = JSON.stringify([request.to]);
} }
$.ajax({ function success() {
dataType: 'json', // This seems to be ignored. We still get back an xhr.
url: '/json/send_message',
type: 'POST',
data: request,
success: function (resp, statusText, xhr) {
$("#new_message_content").val('').focus(); $("#new_message_content").val('').focus();
autosize_textarea(); autosize_textarea();
$("#send-status").hide(0); $("#send-status").hide(0);
@@ -342,18 +367,13 @@ function send_message() {
clear_message_snapshot(); clear_message_snapshot();
$("#compose-send-button").removeAttr('disabled'); $("#compose-send-button").removeAttr('disabled');
$("#sending-indicator").hide(); $("#sending-indicator").hide();
},
error: function (xhr, error_type) {
if (error_type !== 'timeout' && reload.is_pending()) {
// The error might be due to the server changing
reload.initiate({immediate: true, send_after_reload: true});
return;
} }
var response = util.xhr_error_message("Error sending message", xhr);
compose_error(response, $('#new_message_content'));
}
});
if (feature_flags.use_socket) {
send_message_socket(request, success);
} else {
send_message_ajax(request, success);
}
} }
exports.finish = function () { exports.finish = function () {

View File

@@ -35,6 +35,7 @@ exports.left_side_userlist = page_params.staging ||
// Still very beta: // Still very beta:
exports.fade_users_when_composing = page_params.staging || is_customer4; exports.fade_users_when_composing = page_params.staging || is_customer4;
exports.use_socket = page_params.staging;
// Still burning in... // Still burning in...