Skip to content
Browse files

Internet Explorer 9′s problematic console object

tried to use console.log.apply, I got an error. After some quick
investigations, it appeared obvious that the console object being
injected into the window by the developer tools hadn’t received an
update since Internet Explorer 8 and so the methods it contains aren’t
instances of Function like all the most other DOM methods.
  • Loading branch information...
1 parent e77af9a commit 8e2c0d7e4a0a908ae392ebcbb72051b0890b3f00 Chris James committed Jan 29, 2013
Showing with 109 additions and 104 deletions.
  1. +109 −104 client/thunderpush.js
View
213 client/thunderpush.js
@@ -1,105 +1,110 @@
-var Thunder = new function() {
- this.channels = [];
- this.handlers = [];
-
- this.reconnect_delays = [1000, 2500, 5000, 10000, 30000, 60000];
-
- this.options = {
- // verbose?
- log: false
- };
-
- this.connect = function(server, apikey, channels, options) {
- this.server = "http://" + server + "/connect";
- this.apikey = apikey;
- this.channels = channels;
- this.reconnect_tries = 0;
-
- // merge options
- for (var attr in options) {
- this.options[attr] = options[attr];
- }
-
- this.user = this.options.user;
- this.makeConnection();
-
- var that = this;
- };
-
- this.listen = function(handler) {
- this.log("New handler has been registered.");
- this.handlers.push(handler);
- };
-
- this.makeConnection = function() {
- var that = this;
-
- // make a connection
- this.socket = new SockJS(this.server, undefined,
- {'debug': this.options.log});
-
- this.socket.onopen = function() {
- that.log("Connection has been estabilished.");
-
- // reset retries counter
- that.reconnect_tries = 0;
-
- // connect and subscribe to channels
- that.socket.send("CONNECT " + that.user + ":" + that.apikey);
-
- if (that.channels.length)
- that.socket.send("SUBSCRIBE " + that.channels.join(":"));
- }
-
- this.socket.onmessage = function(e) {
- that.log("Message has been received", e.data);
-
- try {
- // try to parse the message as json
- var json_data = JSON.parse(e.data);
- e.data = json_data;
- }
- catch(e) {
- // not json, leave it as is
- }
-
- for (var i = 0; i < that.handlers.length; i++) {
- that.handlers[i](e.data);
- }
- }
-
- this.socket.onclose = function(e) {
- that.log("Connection has been lost.");
-
- if (e.code == 9000 || e.code == 9001 || e.code == 9002) {
- // received "key not good" close message
- that.log("Reconnect supressed because of:", e);
- return;
- }
-
- var delay = that.reconnect_delays[that.reconnect_tries]
- || that.reconnect_delays[that.reconnect_delays.length - 1];
-
- that.log("Reconnecting in", delay, "ms...");
- that.reconnect_tries++;
-
- setTimeout(function() {
- that.makeConnection();
- }, delay);
- }
- };
-
- this.log = function(msg) {
- if (this.options.log
- && "console" in window && "log" in window.console) {
-
- if (arguments.length == 1) {
- console.log(arguments[0]);
- }
- else {
- console.log.apply(console,
- Array.prototype.slice.call(arguments));
- }
- }
- };
+var isMSIE = /*@cc_on!@*/0;
+var Thunder = new function() {
+ this.channels = [];
+ this.handlers = [];
+
+ this.reconnect_delays = [1000, 2500, 5000, 10000, 30000, 60000];
+
+ this.options = {
+ // verbose?
+ log: false
+ };
+
+ this.connect = function(server, apikey, channels, options) {
+ this.server = "http://" + server + "/connect";
+ this.apikey = apikey;
+ this.channels = channels;
+ this.reconnect_tries = 0;
+
+ // merge options
+ for (var attr in options) {
+ this.options[attr] = options[attr];
+ }
+
+ this.user = this.options.user;
+ this.makeConnection();
+
+ var that = this;
+ };
+
+ this.listen = function(handler) {
+ this.log("New handler has been registered.");
+ this.handlers.push(handler);
+ };
+
+ this.makeConnection = function() {
+ var that = this;
+
+ // make a connection
+ this.socket = new SockJS(this.server, undefined,
+ {'debug': this.options.log});
+
+ this.socket.onopen = function() {
+ that.log("Connection has been estabilished.");
+
+ // reset retries counter
+ that.reconnect_tries = 0;
+
+ // connect and subscribe to channels
+ that.socket.send("CONNECT " + that.user + ":" + that.apikey);
+
+ if (that.channels.length)
+ that.socket.send("SUBSCRIBE " + that.channels.join(":"));
+ }
+
+ this.socket.onmessage = function(e) {
+ that.log("Message has been received", e.data);
+
+ try {
+ // try to parse the message as json
+ var json_data = JSON.parse(e.data);
+ e.data = json_data;
+ }
+ catch(e) {
+ // not json, leave it as is
+ }
+
+ for (var i = 0; i < that.handlers.length; i++) {
+ that.handlers[i](e.data);
+ }
+ }
+
+ this.socket.onclose = function(e) {
+ that.log("Connection has been lost.");
+
+ if (e.code == 9000 || e.code == 9001 || e.code == 9002) {
+ // received "key not good" close message
+ that.log("Reconnect supressed because of:", e);
+ return;
+ }
+
+ var delay = that.reconnect_delays[that.reconnect_tries]
+ || that.reconnect_delays[that.reconnect_delays.length - 1];
+
+ that.log("Reconnecting in", delay, "ms...");
+ that.reconnect_tries++;
+
+ setTimeout(function() {
+ that.makeConnection();
+ }, delay);
+ }
+ };
+
+ this.log = function(msg) {
+ if (this.options.log
+ && "console" in window && "log" in window.console) {
+
+ if (arguments.length == 1) {
+ console.log(arguments[0]);
+ }
+ else {
+ if (isMSIE) {
+ var log = Function.prototype.bind.call(console.log, console);
+ log.apply(console, Array.prototype.slice.call(arguments));
+ } else {
+ console.log.apply(console, Array.prototype.slice.call(arguments));
+ }
+ }
+ }
+ };
}

0 comments on commit 8e2c0d7

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