Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 8e2c0d7e4a0a908ae392ebcbb72051b0890b3f00 1 parent e77af9a
Chris James dcrawkstar authored

Showing 1 changed file with 109 additions and 104 deletions. Show diff stats Hide diff stats

  1. +109 104 client/thunderpush.js
213 client/thunderpush.js
... ... @@ -1,105 +1,110 @@
1   -var Thunder = new function() {
2   - this.channels = [];
3   - this.handlers = [];
4   -
5   - this.reconnect_delays = [1000, 2500, 5000, 10000, 30000, 60000];
6   -
7   - this.options = {
8   - // verbose?
9   - log: false
10   - };
11   -
12   - this.connect = function(server, apikey, channels, options) {
13   - this.server = "http://" + server + "/connect";
14   - this.apikey = apikey;
15   - this.channels = channels;
16   - this.reconnect_tries = 0;
17   -
18   - // merge options
19   - for (var attr in options) {
20   - this.options[attr] = options[attr];
21   - }
22   -
23   - this.user = this.options.user;
24   - this.makeConnection();
25   -
26   - var that = this;
27   - };
28   -
29   - this.listen = function(handler) {
30   - this.log("New handler has been registered.");
31   - this.handlers.push(handler);
32   - };
33   -
34   - this.makeConnection = function() {
35   - var that = this;
36   -
37   - // make a connection
38   - this.socket = new SockJS(this.server, undefined,
39   - {'debug': this.options.log});
40   -
41   - this.socket.onopen = function() {
42   - that.log("Connection has been estabilished.");
43   -
44   - // reset retries counter
45   - that.reconnect_tries = 0;
46   -
47   - // connect and subscribe to channels
48   - that.socket.send("CONNECT " + that.user + ":" + that.apikey);
49   -
50   - if (that.channels.length)
51   - that.socket.send("SUBSCRIBE " + that.channels.join(":"));
52   - }
53   -
54   - this.socket.onmessage = function(e) {
55   - that.log("Message has been received", e.data);
56   -
57   - try {
58   - // try to parse the message as json
59   - var json_data = JSON.parse(e.data);
60   - e.data = json_data;
61   - }
62   - catch(e) {
63   - // not json, leave it as is
64   - }
65   -
66   - for (var i = 0; i < that.handlers.length; i++) {
67   - that.handlers[i](e.data);
68   - }
69   - }
70   -
71   - this.socket.onclose = function(e) {
72   - that.log("Connection has been lost.");
73   -
74   - if (e.code == 9000 || e.code == 9001 || e.code == 9002) {
75   - // received "key not good" close message
76   - that.log("Reconnect supressed because of:", e);
77   - return;
78   - }
79   -
80   - var delay = that.reconnect_delays[that.reconnect_tries]
81   - || that.reconnect_delays[that.reconnect_delays.length - 1];
82   -
83   - that.log("Reconnecting in", delay, "ms...");
84   - that.reconnect_tries++;
85   -
86   - setTimeout(function() {
87   - that.makeConnection();
88   - }, delay);
89   - }
90   - };
91   -
92   - this.log = function(msg) {
93   - if (this.options.log
94   - && "console" in window && "log" in window.console) {
95   -
96   - if (arguments.length == 1) {
97   - console.log(arguments[0]);
98   - }
99   - else {
100   - console.log.apply(console,
101   - Array.prototype.slice.call(arguments));
102   - }
103   - }
104   - };
  1 +var isMSIE = /*@cc_on!@*/0;
  2 +var Thunder = new function() {
  3 + this.channels = [];
  4 + this.handlers = [];
  5 +
  6 + this.reconnect_delays = [1000, 2500, 5000, 10000, 30000, 60000];
  7 +
  8 + this.options = {
  9 + // verbose?
  10 + log: false
  11 + };
  12 +
  13 + this.connect = function(server, apikey, channels, options) {
  14 + this.server = "http://" + server + "/connect";
  15 + this.apikey = apikey;
  16 + this.channels = channels;
  17 + this.reconnect_tries = 0;
  18 +
  19 + // merge options
  20 + for (var attr in options) {
  21 + this.options[attr] = options[attr];
  22 + }
  23 +
  24 + this.user = this.options.user;
  25 + this.makeConnection();
  26 +
  27 + var that = this;
  28 + };
  29 +
  30 + this.listen = function(handler) {
  31 + this.log("New handler has been registered.");
  32 + this.handlers.push(handler);
  33 + };
  34 +
  35 + this.makeConnection = function() {
  36 + var that = this;
  37 +
  38 + // make a connection
  39 + this.socket = new SockJS(this.server, undefined,
  40 + {'debug': this.options.log});
  41 +
  42 + this.socket.onopen = function() {
  43 + that.log("Connection has been estabilished.");
  44 +
  45 + // reset retries counter
  46 + that.reconnect_tries = 0;
  47 +
  48 + // connect and subscribe to channels
  49 + that.socket.send("CONNECT " + that.user + ":" + that.apikey);
  50 +
  51 + if (that.channels.length)
  52 + that.socket.send("SUBSCRIBE " + that.channels.join(":"));
  53 + }
  54 +
  55 + this.socket.onmessage = function(e) {
  56 + that.log("Message has been received", e.data);
  57 +
  58 + try {
  59 + // try to parse the message as json
  60 + var json_data = JSON.parse(e.data);
  61 + e.data = json_data;
  62 + }
  63 + catch(e) {
  64 + // not json, leave it as is
  65 + }
  66 +
  67 + for (var i = 0; i < that.handlers.length; i++) {
  68 + that.handlers[i](e.data);
  69 + }
  70 + }
  71 +
  72 + this.socket.onclose = function(e) {
  73 + that.log("Connection has been lost.");
  74 +
  75 + if (e.code == 9000 || e.code == 9001 || e.code == 9002) {
  76 + // received "key not good" close message
  77 + that.log("Reconnect supressed because of:", e);
  78 + return;
  79 + }
  80 +
  81 + var delay = that.reconnect_delays[that.reconnect_tries]
  82 + || that.reconnect_delays[that.reconnect_delays.length - 1];
  83 +
  84 + that.log("Reconnecting in", delay, "ms...");
  85 + that.reconnect_tries++;
  86 +
  87 + setTimeout(function() {
  88 + that.makeConnection();
  89 + }, delay);
  90 + }
  91 + };
  92 +
  93 + this.log = function(msg) {
  94 + if (this.options.log
  95 + && "console" in window && "log" in window.console) {
  96 +
  97 + if (arguments.length == 1) {
  98 + console.log(arguments[0]);
  99 + }
  100 + else {
  101 + if (isMSIE) {
  102 + var log = Function.prototype.bind.call(console.log, console);
  103 + log.apply(console, Array.prototype.slice.call(arguments));
  104 + } else {
  105 + console.log.apply(console, Array.prototype.slice.call(arguments));
  106 + }
  107 + }
  108 + }
  109 + };
105 110 }

0 comments on commit 8e2c0d7

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