Commit dce9048a authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

adding sendMessage method on javascript library

parent 73100936
......@@ -92,7 +92,7 @@
$("#sendButton").click(function(){
if (($("#nick").val() != "") && ($("#message").val() != "") && ($("#room").val() != "")) {
$.post( '/pub?id=' + $("#room").val(), '{"nick":"' + $("#nick").val() + '", "text":"' + $("#message").val() + '"}', onSendText);
pushstream.sendMessage('{"nick":"' + $("#nick").val() + '", "text":"' + $("#message").val() + '"}', onSendText);
} else {
alert("nick, room and text are required");
}
......
......@@ -92,7 +92,7 @@
$("#sendButton").click(function(){
if (($("#nick").val() != "") && ($("#message").val() != "") && ($("#room").val() != "")) {
$.post( '/pub?id=' + $("#room").val(), '{"nick":"' + $("#nick").val() + '", "text":"' + $("#message").val() + '"}', onSendText);
pushstream.sendMessage('{"nick":"' + $("#nick").val() + '", "text":"' + $("#message").val() + '"}', onSendText);
} else {
alert("nick, room and text are required");
}
......
......@@ -53,7 +53,7 @@
};
var Ajax = {
getXHRObject : function() {
_getXHRObject : function() {
var xhr = false;
try { xhr = new window.ActiveXObject("Msxml2.XMLHTTP"); }
catch (e1) {
......@@ -67,15 +67,15 @@
}
return xhr;
},
load : function (settings) {
_send : function(settings, post) {
settings = settings || {};
var cache = settings.cache || true;
var xhr = Ajax.getXHRObject();
var xhr = Ajax._getXHRObject();
if (!xhr||!settings.url) return;
var url = settings.url + ((cache) ? "" : settings.url + ((settings.url.indexOf("?")+1) ? "&" : "?") + "_=" + new Date().getTime());
var url = settings.url + ((cache || post) ? "" : settings.url + ((settings.url.indexOf("?")+1) ? "&" : "?") + "_=" + new Date().getTime());
xhr.open("GET", url, true);
xhr.open(((post) ? "POST" : "GET"), url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
......@@ -88,8 +88,19 @@
}
}
if (settings.beforeSend) settings.beforeSend(xhr);
xhr.send(null);
if (post) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
xhr.send((post) ? settings.data : null);
return xhr;
},
load : function(settings) {
return Ajax._send(settings, false);
},
post : function(settings) {
return Ajax._send(settings, true);
}
};
......@@ -117,6 +128,22 @@
return url;
};
var getPublisherUrl = function(pushstream) {
var channel = "";
var url = (pushstream.useSSL) ? "https://" : "http://";
url += pushstream.host;
url += ((pushstream.port != 80) && (pushstream.port != 443)) ? (":" + pushstream.port) : "";
url += pushstream.urlPrefixPublisher;
for (var channelName in pushstream.channels) {
if (!pushstream.channels.hasOwnProperty || pushstream.channels.hasOwnProperty(channelName)) {
channel = channelName;
break;
}
}
url += "?id=" + channel;
return url;
};
var extract_xss_domain = function(domain) {
// if domain is a ip address return it, else return the last two parts of it
return (domain.match(/^(\d{1,3}\.){3}\d{1,3}$/)) ? domain : domain.split('.').slice(-2).join('.');
......@@ -409,6 +436,7 @@
this.reconnecttimer = null;
this.urlPrefixPublisher = settings.urlPrefixPublisher || '/pub';
this.urlPrefixStream = settings.urlPrefixStream || '/sub';
this.urlPrefixEventsource = settings.urlPrefixEventsource || '/ev';
this.urlPrefixLongpolling = settings.urlPrefixLongpolling || '/lp';
......@@ -562,9 +590,22 @@
Log4js.info("trying to reconnect in", timeout);
this.reconnecttimer = setTimeout(linker(this._connect, this), timeout);
}
},
sendMessage: function(message, successCallback, errorCallback) {
if (this.wrapper.type === WebSocketWrapper.TYPE) {
this.wrapper.sendMessage(message);
if (successCallback) successCallback();
} else {
Ajax.post({url: getPublisherUrl(this), data: message, success: successCallback, error: errorCallback});
}
}
};
PushStream.sendMessage = function(url, message, successCallback, errorCallback) {
Ajax.post({url: url, data: message, success: successCallback, error: errorCallback});
};
// to make server header template more clear, it calls register and
// by a url parameter we find the stream wrapper instance
PushStream.register = function(iframe) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment