github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/lib/info-iframe.js (about) 1 'use strict'; 2 3 var EventEmitter = require('events').EventEmitter 4 , inherits = require('inherits') 5 , JSON3 = require('json3') 6 , utils = require('./utils/event') 7 , IframeTransport = require('./transport/iframe') 8 , InfoReceiverIframe = require('./info-iframe-receiver') 9 ; 10 11 var debug = function() {}; 12 if (process.env.NODE_ENV !== 'production') { 13 debug = require('debug')('sockjs-client:info-iframe'); 14 } 15 16 function InfoIframe(baseUrl, url) { 17 var self = this; 18 EventEmitter.call(this); 19 20 var go = function() { 21 var ifr = self.ifr = new IframeTransport(InfoReceiverIframe.transportName, url, baseUrl); 22 23 ifr.once('message', function(msg) { 24 if (msg) { 25 var d; 26 try { 27 d = JSON3.parse(msg); 28 } catch (e) { 29 debug('bad json', msg); 30 self.emit('finish'); 31 self.close(); 32 return; 33 } 34 35 var info = d[0], rtt = d[1]; 36 self.emit('finish', info, rtt); 37 } 38 self.close(); 39 }); 40 41 ifr.once('close', function() { 42 self.emit('finish'); 43 self.close(); 44 }); 45 }; 46 47 // TODO this seems the same as the 'needBody' from transports 48 if (!global.document.body) { 49 utils.attachEvent('load', go); 50 } else { 51 go(); 52 } 53 } 54 55 inherits(InfoIframe, EventEmitter); 56 57 InfoIframe.enabled = function() { 58 return IframeTransport.enabled(); 59 }; 60 61 InfoIframe.prototype.close = function() { 62 if (this.ifr) { 63 this.ifr.close(); 64 } 65 this.removeAllListeners(); 66 this.ifr = null; 67 }; 68 69 module.exports = InfoIframe;