[101] | 1 | (function() {
|
---|
| 2 | 'use strict';
|
---|
| 3 | var $asm = {};
|
---|
| 4 | ss.initAssembly($asm, 'NiseWebSocket');
|
---|
| 5 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 6 | // CloseEvent
|
---|
| 7 | var $CloseEvent = function(code, reason) {
|
---|
| 8 | this.code = 0;
|
---|
| 9 | this.reason = null;
|
---|
| 10 | $NiseEvent.call(this, 'close');
|
---|
| 11 | this.code = code;
|
---|
| 12 | this.reason = reason;
|
---|
| 13 | };
|
---|
| 14 | $CloseEvent.__typeName = 'CloseEvent';
|
---|
| 15 | global.CloseEvent = $CloseEvent;
|
---|
| 16 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 17 | // MessageEvent
|
---|
| 18 | var $MessageEvent = function(data) {
|
---|
| 19 | this.data = null;
|
---|
| 20 | $NiseEvent.call(this, 'message');
|
---|
| 21 | this.data = data;
|
---|
| 22 | };
|
---|
| 23 | $MessageEvent.__typeName = 'MessageEvent';
|
---|
| 24 | global.MessageEvent = $MessageEvent;
|
---|
| 25 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 26 | // NiseEvent
|
---|
| 27 | var $NiseEvent = function(type) {
|
---|
| 28 | this.type = null;
|
---|
| 29 | this.type = type;
|
---|
| 30 | };
|
---|
| 31 | $NiseEvent.__typeName = 'NiseEvent';
|
---|
| 32 | global.NiseEvent = $NiseEvent;
|
---|
| 33 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 34 | // NiseWebSocket
|
---|
| 35 | var $NiseWebSocket = function(url) {
|
---|
| 36 | this.$_Url = null;
|
---|
| 37 | this.$_Protocols = null;
|
---|
| 38 | this.$_ReadyState = 1;
|
---|
| 39 | this.$_PollingTimer = 0;
|
---|
| 40 | this.binaryType = null;
|
---|
| 41 | this.bufferedAmount = 0;
|
---|
| 42 | this.extensions = null;
|
---|
| 43 | this.onclose = null;
|
---|
| 44 | this.onerror = null;
|
---|
| 45 | this.onmessage = null;
|
---|
| 46 | this.onopen = null;
|
---|
| 47 | this.protocol = null;
|
---|
| 48 | this.readyState = 0;
|
---|
| 49 | this.url = null;
|
---|
| 50 | var opentimer = 0;
|
---|
| 51 | this.$_ReadyState = 0;
|
---|
| 52 | this.$_Url = url.replace('ws://', 'http://');
|
---|
| 53 | opentimer = window.setTimeout(ss.mkdel(this, function() {
|
---|
| 54 | window.clearTimeout(opentimer);
|
---|
| 55 | this.$opend();
|
---|
| 56 | }), 1000);
|
---|
| 57 | };
|
---|
| 58 | $NiseWebSocket.__typeName = 'NiseWebSocket';
|
---|
| 59 | $NiseWebSocket.$ctor1 = function(url, protocols) {
|
---|
| 60 | $NiseWebSocket.call(this, url);
|
---|
| 61 | this.$_Protocols = protocols.split(String.fromCharCode(44));
|
---|
| 62 | };
|
---|
| 63 | $NiseWebSocket.$ctor2 = function(url, protocols) {
|
---|
| 64 | $NiseWebSocket.call(this, url);
|
---|
| 65 | this.$_Protocols = protocols;
|
---|
| 66 | };
|
---|
| 67 | global.NiseWebSocket = $NiseWebSocket;
|
---|
| 68 | ss.initClass($NiseEvent, $asm, {});
|
---|
| 69 | ss.initClass($CloseEvent, $asm, {}, $NiseEvent);
|
---|
| 70 | ss.initClass($MessageEvent, $asm, {}, $NiseEvent);
|
---|
| 71 | ss.initClass($NiseWebSocket, $asm, {
|
---|
| 72 | $opend: function() {
|
---|
| 73 | this.$_ReadyState = 1;
|
---|
| 74 | var oe = new $NiseEvent('open');
|
---|
| 75 | if (!ss.staticEquals(this.onopen, null)) {
|
---|
| 76 | this.onopen(oe);
|
---|
| 77 | }
|
---|
| 78 | this.$_PollingTimer = window.setInterval(ss.mkdel(this, function() {
|
---|
| 79 | $.ajax({
|
---|
| 80 | url: this.$_Url,
|
---|
| 81 | type: 'POST',
|
---|
| 82 | dataType: 'json',
|
---|
| 83 | data: '{"brk_wai":"Polling"}',
|
---|
| 84 | success: ss.mkdel(this, function(respo, textStatus, request) {
|
---|
| 85 | if (ss.isValue(respo)) {
|
---|
| 86 | var e = new $MessageEvent(request.responseText);
|
---|
| 87 | if (!ss.staticEquals(this.onmessage, null)) {
|
---|
| 88 | this.onmessage(e);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }),
|
---|
| 92 | error: function(request1, textStatus1, error) {
|
---|
| 93 | }
|
---|
| 94 | });
|
---|
| 95 | }), 1000);
|
---|
| 96 | },
|
---|
| 97 | close: function() {
|
---|
| 98 | if (this.$_PollingTimer !== 0) {
|
---|
| 99 | window.clearInterval(this.$_PollingTimer);
|
---|
| 100 | this.$_PollingTimer = 0;
|
---|
| 101 | }
|
---|
| 102 | var closetimer = 0;
|
---|
| 103 | this.$_ReadyState = 2;
|
---|
| 104 | closetimer = window.setTimeout(ss.mkdel(this, function() {
|
---|
| 105 | window.clearTimeout(closetimer);
|
---|
| 106 | this.$closed($CloseEvent.closE_NORMAL, '');
|
---|
| 107 | }), 1000);
|
---|
| 108 | },
|
---|
| 109 | $closed: function(code, reason) {
|
---|
| 110 | this.$_ReadyState = 3;
|
---|
| 111 | var oe = new $CloseEvent(code, reason);
|
---|
| 112 | if (!ss.staticEquals(this.onclose, null)) {
|
---|
| 113 | this.onclose(oe);
|
---|
| 114 | }
|
---|
| 115 | },
|
---|
| 116 | send: function(data) {
|
---|
| 117 | $.ajax({ url: this.$_Url, type: 'POST', data: data, dataType: 'json', success: ss.mkdel(this, function(respo, textStatus, request) {
|
---|
| 118 | if (ss.isValue(respo)) {
|
---|
| 119 | var e = new $MessageEvent(request.responseText);
|
---|
| 120 | if (!ss.staticEquals(this.onmessage, null)) {
|
---|
| 121 | this.onmessage(e);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }), error: ss.mkdel(this, function(request1, textStatus1, error) {
|
---|
| 125 | var e1 = new $NiseEvent('error');
|
---|
| 126 | this.onerror(e1);
|
---|
| 127 | }) });
|
---|
| 128 | }
|
---|
| 129 | });
|
---|
| 130 | $NiseWebSocket.$ctor1.prototype = $NiseWebSocket.$ctor2.prototype = $NiseWebSocket.prototype;
|
---|
| 131 | (function() {
|
---|
| 132 | $CloseEvent.closE_NORMAL = 1000;
|
---|
| 133 | $CloseEvent.closE_GOING_AWAY = 1001;
|
---|
| 134 | $CloseEvent.closE_PROTOCOL_ERROR = 1002;
|
---|
| 135 | $CloseEvent.closE_UNSUPPORTED = 1003;
|
---|
| 136 | $CloseEvent.closE_TOO_LARGE = 1004;
|
---|
| 137 | $CloseEvent.closE_NO_STATUS = 1005;
|
---|
| 138 | $CloseEvent.closE_ABNORMAL = 1006;
|
---|
| 139 | })();
|
---|
| 140 | (function() {
|
---|
| 141 | $NiseWebSocket.CLOSED = 3;
|
---|
| 142 | $NiseWebSocket.CLOSING = 2;
|
---|
| 143 | $NiseWebSocket.CONNECTING = 0;
|
---|
| 144 | $NiseWebSocket.OPEN = 1;
|
---|
| 145 | })();
|
---|
| 146 | })();
|
---|