Created
May 8, 2012 13:51
-
-
Save rpgmaker/2635210 to your computer and use it in GitHub Desktop.
Initial JS API Rewrite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.format = function (text) { | |
if (arguments.length <= 1) | |
return text; | |
var tokenCount = arguments.length - 2; | |
for (var token = 0; token <= tokenCount; token++) { | |
text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]); | |
} | |
return text; | |
}; | |
String.prototype.endsWith = function (suffix) { | |
return this.indexOf(suffix, this.length - suffix.length) !== -1; | |
}; | |
jQuery.support.cors = true; | |
(function (jQuery) { | |
if (window.XDomainRequest) { | |
jQuery.ajaxTransport(function (s) { | |
if (s.crossDomain && s.async) { | |
if (s.timeout) { | |
s.xdrTimeout = s.timeout; | |
delete s.timeout; | |
} | |
var xdr; | |
return { | |
send: function (_, complete) { | |
function callback(status, statusText, responses, responseHeaders) { | |
xdr.onload = xdr.onerror = xdr.ontimeout = jQuery.noop; | |
xdr = undefined; | |
complete(status, statusText, responses, responseHeaders); | |
} | |
xdr = new XDomainRequest(); | |
xdr.open(s.type, s.url); | |
xdr.onload = function () { | |
callback(200, "OK", { text: xdr.responseText }, "Content-Type: " + xdr.contentType); | |
}; | |
xdr.onerror = function () { | |
callback(404, "Not Found"); | |
}; | |
if (s.xdrTimeout) { | |
xdr.ontimeout = function () { | |
callback(0, "timeout"); | |
}; | |
xdr.timeout = s.xdrTimeout; | |
} | |
xdr.send((s.hasContent && s.data) || null); | |
}, | |
abort: function () { | |
if (xdr) { | |
xdr.onerror = jQuery.noop(); | |
xdr.abort(); | |
} | |
} | |
}; | |
} | |
}); | |
} | |
})(jQuery); | |
PSERVICEBUS = PSERVICEBUS || {} | |
(function (fn) { | |
var endpointAddress = "http://localhost:8087/ESBRestService/", | |
websocketAddress = "", | |
websocketPort = "8081", | |
rethrow = false, | |
apikey = "demo", | |
passcode = "demoe", | |
invalidstate = 1; | |
fn.ondisconnect = function () { }; | |
fn.onconnect = function () { }; | |
fn.connected = true; | |
fn.transport = { | |
msmq: 0, | |
rabbitmq: 1, | |
ravendb: 2, | |
tcp: 3, | |
redis: 7 | |
} | |
fn.format = { | |
json: 1 | |
}; | |
var ajax = (function () { | |
var useJsonp = false, | |
fail = function (e) { | |
if (typeof (e) !== "string") return; | |
throw e; | |
}; | |
return { | |
helper: (function () { | |
var useJsonp = false, | |
fail = function (e) { | |
if (typeof (e) !== "string") return; | |
throw e; | |
}, self = this; | |
return { | |
ajaxJsonp: function (method, data, success) { | |
fn.connected = !(method === "Disconnect"); | |
data.ReThrowException = rethrow.toString(); | |
data.ESBUserName = apikey; | |
data.ESBPassword = passcode; | |
var url = String.format("{0}{1}?callback=?", endpointAddress, method); | |
$.ajax({ | |
url: url, | |
data: data, | |
dataType: 'jsonp', | |
crossDomain: true, | |
contentType: 'application/json; charset=utf-8', | |
success: function (result) { | |
if (success) success(result); | |
}, | |
error: function (e) { | |
if (fail) fail(e); | |
} | |
}); | |
}, | |
ajax: function (method, data, success, async) { | |
fn.connected = !(method === "Disconnect"); | |
async = async || false; | |
var url = String.format("{0}{1}?ReThrowException={2}&ESBUserName={3}&ESBPassword={4}", | |
endpointAddress, method, rethrow, apikey, passcode); | |
$.ajax({ | |
type: 'POST', | |
url: url, | |
data: data, | |
traditional: true, | |
dataType: 'json', | |
async: async, | |
success: function (result, status, xhr) { | |
if (result) { | |
var d = result.d || result; | |
if (d) { | |
var evald = JSON.parse(d); | |
if (success) success(evald); | |
} | |
} | |
}, | |
error: function (xhr, e, msg) { | |
if (!useJsonp) { | |
useJsonp = true; | |
self.ajaxJsonp(method, data, success); | |
return; | |
} | |
if (fail) fail(xhr.responseText); | |
} | |
}); | |
} | |
}; | |
})() | |
}; | |
})(); | |
fn.setting = (function (fn) { | |
return { | |
apikey: function (value) { apikey = value; }, | |
passcode: function (value) { passcode = value; }, | |
rethrow: function (value) { rethrow = value; }, | |
websocketPort: function (port) { websocketPort = port; }, | |
endpoint: function (value) { endpointAddress = value; }, | |
websocket: function () { | |
if (websocketAddress) return websocketAddress; | |
var url = endpointAddress.replace("//", "\\").split("/")[0], | |
tokens = url.split(":"); | |
tokens[tokens.length - 1] = websocketPort; | |
tokens[0] = "ws"; | |
url = tokens.join(":"); | |
return url.replace("\\", "//"); | |
}, | |
transport: (function () { | |
var address = "localhost:5672&userID=guest;password=guest", | |
parseEndpoint = function (topicName, subscriberName) { | |
return address + ";queue=" + topicName + subscriberName; | |
}; | |
return { | |
address: function (value) { address = value; }, | |
type: fn.transport.rabbitMQ, | |
info: function (topicName, subscriberName) { | |
switch (this.type) { | |
case fn.transport.msmq: | |
case fn.transport.rabbitmq: | |
case fn.transport.redis: | |
return { | |
Format: fn.format.json, | |
Path: parseEndpoint(topicName, subscriberName) | |
} | |
break; | |
case fn.transport.ravendb: | |
return { | |
Format: fn.format.json, | |
ConnectionString: parseEndpoint(topicName, subscriberName) | |
} | |
break; | |
case fn.transport.tcp: | |
var tokens = address.split(';'), | |
ipTokens = tokens[0].split(':'), | |
useSSL = tokens.length > 1 ? | |
(true == tokens[1]) : false, | |
ipAddress = ipTokens[0], | |
port = parseInt(ipTokens[1]); | |
return { | |
Format: fn.format.json, | |
IPAddress: ipAddress, Port: port, | |
UseSSL: useSSL | |
} | |
break; | |
} | |
} | |
} | |
})() | |
} | |
})(); | |
fn.register = function (data) { | |
if (!data.topic || !data.info) | |
throw "topic and info property need to be set for register method"; | |
ajax.helper.ajax("RegisterTopic", | |
{ topicData: JSON.stringify({ | |
ContractDict: info, | |
TopicName: data.topic, | |
TopicDescription: data.description || data.topic | |
}) | |
}); | |
} | |
})(PSERVICEBUS); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment