LodopFuncs.js 3.35 KB
Newer Older
krysent's avatar
krysent committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
//用双端口加载主JS文件Lodop.js(或CLodopfuncs.js兼容老版本)以防其中某端口被占:
var MainJS = "CLodopfuncs.js",
  URL_WS1 = "ws://localhost:8000/" + MainJS, //ws用8000/18000
  URL_WS2 = "ws://localhost:18000/" + MainJS,
  URL_HTTP1 = "http://localhost:8000/" + MainJS, //http用8000/18000
  URL_HTTP2 = "http://localhost:18000/" + MainJS,
  URL_HTTP3 = "https://localhost.lodop.net:8443/" + MainJS; //https用8000/8443

//==检查加载成功与否,如没成功则用http(s)再试==
//==低版本CLODOP6.561/Lodop7.043及前)用本方法==
function checkOrTryHttp() {
  if (window.getCLodop) return true;
  var head =
    document.head ||
    document.getElementsByTagName("head")[0] ||
    document.documentElement;
  var JS1 = document.createElement("script"),
    JS2 = document.createElement("script"),
    JS3 = document.createElement("script");
  JS1.src = URL_HTTP1;
  JS2.src = URL_HTTP2;
  JS3.src = URL_HTTP3;
  JS1.onerror = function (e) {
    if (window.location.protocol !== "https:")
      head.insertBefore(JS2, head.firstChild);
    else head.insertBefore(JS3, head.firstChild);
  };
  JS2.onerror = JS3.onerror = function () {
    var JSelf = document.createElement("script");
    JSelf.src = "/" + MainJS; //JSelf让其它电脑通过本机打印(仅适用CLodop自带例子)
    document.head.insertBefore(JSelf, document.head.firstChild);
  };
  head.insertBefore(JS1, head.firstChild);
}

//==加载Lodop对象的主过程:==
(function loadCLodop() {
  if (!window.WebSocket && window.MozWebSocket)
    window.WebSocket = window.MozWebSocket;
  //ws方式速度快(小于200ms)且可避免CORS错误,但要求Lodop版本足够新:
  try {
    var WSK1 = new WebSocket(URL_WS1);
    WSK1.onopen = function (e) {
      setTimeout("checkOrTryHttp()", 200);
    };
    WSK1.onmessage = function (e) {
      if (!window.getCLodop) eval(e.data);
    };
    WSK1.onerror = function (e) {
      var WSK2 = new WebSocket(URL_WS2);
      WSK2.onopen = function (e) {
        setTimeout("checkOrTryHttp()", 200);
      };
      WSK2.onmessage = function (e) {
        if (!window.getCLodop) eval(e.data);
      };
      WSK2.onerror = function (e) {
        checkOrTryHttp();
      };
    };
  } catch (e) {
    checkOrTryHttp();
  }
})();

//==获取LODOP对象的主过程:==
function getLodop(oOBJECT, oEMBED) {
  var LODOP;
  try {
    LODOP = window.getCLodop();
    if (!LODOP && document.readyState !== "complete") {
      alert("C-Lodop没准备好,请稍后再试!");
      return;
    }
    //清理原例子内的object或embed元素,避免乱提示:
    if (oEMBED && oEMBED.parentNode) oEMBED.parentNode.removeChild(oEMBED);
    if (oOBJECT && oOBJECT.parentNode) oOBJECT.parentNode.removeChild(oOBJECT);
    return LODOP;
  } catch (err) {
    alert("getLodop出错:" + err);
  }
}

/* 
 *             佛曰:
 *                     写字楼里写字间,写字间里程序员;
 *                     程序人员写程序,又拿程序换酒钱
 *                     酒醒只在网上坐,酒醉还来网下眠
 *                     酒醉酒醒日复日,网上网下年复年
 *                     但愿老死电脑间,不愿鞠躬老板前
 *                     奔驰宝马贵者趣,公交自行程序员
 *                     别人笑我忒疯癫,我笑自己命太贱;
 *                     不见满街漂亮妹,哪个归得程序员?
 */