| 6 | var _pending = []; |
| 7 | |
| 8 | function getIO() { |
| 9 | return new Promise(function (resolve, reject) { |
| 10 | if (typeof io == 'function') { |
| 11 | resolve(io); |
| 12 | } else if (typeof document == 'undefined') { |
| 13 | reject(new Error("No DOM document found")); |
| 14 | } else { |
| 15 | _pending.push(resolve); |
| 16 | |
| 17 | if (!_script) { |
| 18 | _script = document.createElement('script'); |
| 19 | _script.onload = _script.onreadystatechange = function () { |
| 20 | if (typeof io == 'function') { |
| 21 | _script.onload = _script.onreadystatechange = null; |
| 22 | var cbs = []; |
| 23 | _pending = []; |
| 24 | cbs.forEach(function (resolve) { |
| 25 | resolve(io); |
| 26 | }); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | _script.src = '/socket.io/socket.io.js'; |
| 31 | document.getElementsByTagName('head')[0].appendChild(_script); |
| 32 | } |
| 33 | } |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | function connect(namespace) { |
| 38 | return getIO().then(function (io) { |