Skip to content
Browse files

introduced isHostMethod to detect websockets

  • Loading branch information...
1 parent 8f5de8f commit 11fe99b03b5444985b8f5f62278f7ef8843f1a97 @krukow committed Jul 29, 2010
Showing with 23 additions and 10 deletions.
  1. +2 −2 example/transactional-chat.html
  2. +21 −8 src/stomple.js
View
4 example/transactional-chat.html
@@ -84,8 +84,8 @@
success: function() {//did subscription succeed?
console.log("sub ok..");
},
- failure: function() {//did subscription fail?
- console.log("sub fail");
+ failure: function(reason) {//did subscription fail?
+ console.log(reason);
}
});
document.getElementById('inputText').focus();
View
29 src/stomple.js
@@ -33,12 +33,24 @@
*/
(function() {
var globalObject = this,
+ /** David Mark's isHostMethod function,
+ * see also:
+ * http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting
+ * Modified to use strict equality
+ */
+ isHostMethod = function(object, property) {
+ var t = typeof object[property];
+ return t==='function' ||
+ (!!(t==='object' && object[property])) ||
+ t==='unknown';
+ },
Stomple,//publicly exported API or 'false' if unsupported.
emptyFn = function() {},
- warnAvailable = globalObject.console && typeof globalObject.console.warn === 'function',
- infoAvailable = globalObject.console && typeof globalObject.console.info === 'function',
+ consoleAvailable = isHostMethod(globalObject,"console"),
+ warnAvailable = consoleAvailable && isHostMethod(globalObject.console,"warn"),
+ infoAvailable = consoleAvailable && isHostMethod(globalObject.console,"info"),
trim;
- if (typeof WebSocket === 'undefined' || !WebSocket || globalObject.Stomple) {
+ if (isHostMethod(globalObject,"WebSocket")) {
if (warnAvailable) {
globalObject.console.warn(globalObject.Stomple?"Stomple already defined.":"Stomple: WebSockets not available");
}
@@ -173,7 +185,8 @@
};
- var NULL = '\u0000';
+ var NULL = '\u0000',
+ NL = '\n';
/**
* Prototype of frame objects. Defines a toString method to convert the frame
@@ -192,18 +205,18 @@
var res = this.command,
h,
hds = this.headers;
- res += '\n';
+ res += NL;
if (hds) {
for (h in hds) {
if(hds.hasOwnProperty(h)) {
res += h;
res += ':';
res += hds[h];
- res += '\n';
+ res += NL;
}
}
}
- res += '\n';
+ res += NL;
if (this.body) {
res += this.body;
}
@@ -983,7 +996,7 @@
return;//cancel
}
var data = msg.data,
- i = data.indexOf('\n'),
+ i = data.indexOf(NL),
N,
cmd = data.substring(0,i),
headersAndBody = data.substring(i+1),

0 comments on commit 11fe99b

Please sign in to comment.
Something went wrong with that request. Please try again.