123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- var __reflect = (this && this.__reflect) || function (p, c, t) {
- p.__class__ = c, t ? t.push(c) : t = [c], p.__types__ = p.__types__ ? t.concat(p.__types__) : t;
- };
- var __extends = this && this.__extends || function __extends(t, e) {
- function r() {
- this.constructor = t;
- }
- for (var i in e) e.hasOwnProperty(i) && (t[i] = e[i]);
- r.prototype = e.prototype, t.prototype = new r();
- };
- //////////////////////////////////////////////////////////////////////////////////////
- //
- // Copyright (c) 2014-present, Egret Technology.
- // All rights reserved.
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above copyright
- // notice, this list of conditions and the following disclaimer in the
- // documentation and/or other materials provided with the distribution.
- // * Neither the name of the Egret nor the
- // names of its contributors may be used to endorse or promote products
- // derived from this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
- // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
- // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- //////////////////////////////////////////////////////////////////////////////////////
- var egret;
- (function (egret) {
- })(egret || (egret = {}));
- //////////////////////////////////////////////////////////////////////////////////////
- //
- // Copyright (c) 2014-present, Egret Technology.
- // All rights reserved.
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above copyright
- // notice, this list of conditions and the following disclaimer in the
- // documentation and/or other materials provided with the distribution.
- // * Neither the name of the Egret nor the
- // names of its contributors may be used to endorse or promote products
- // derived from this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
- // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
- // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- //////////////////////////////////////////////////////////////////////////////////////
- var egret;
- (function (egret) {
- /**
- * The egret.WebSocket class enables code to establish a TCP socket connection, for sending and receiving character string or binary data.
- * To use the methods of the egret.WebSocket class, first use the constructor function new egret.WebSocket to create an egret.WebSocket object.
- * The socket transmits and receives data in asynchronous mode.
- * @event egret.Event.CONNECT Successfully connect to the server。
- * @event egret.ProgressEvent.SOCKET_DATA Receiving server data。
- * @event egret.Event.CLOSE Dispatched when the server closes the connection.
- * @event egret.ProgressEvent Dispatched when an IO error causes a send or load operation to fail.
- * @see http://edn.egret.com/cn/docs/page/602 WebSocket
- * @version Egret 2.4
- * @platform Web,Native
- * @includeExample extension/socket/WebSocket.ts
- * @language en_US
- */
- /**
- * egret.WebSocket 类启用代码以建立传输控制协议 (TCP) 套接字连接,用于发送和接收字符串或二进制数据。
- * 要使用 egret.WebSocket 类的方法,请先使用构造函数 new egret.WebSocket 创建一个 egret.WebSocket 对象。
- * 套接字以异步方式传输和接收数据。
- * @event egret.Event.CONNECT 连接服务器成功。
- * @event egret.ProgressEvent.SOCKET_DATA 接收服务器数据。
- * @event egret.Event.CLOSE 在服务器关闭连接时调度。
- * @event egret.IOErrorEvent.IO_ERROR 在出现输入/输出错误并导致发送或加载操作失败时调度。。
- * @see http://edn.egret.com/cn/docs/page/602 WebSocket
- * @version Egret 2.4
- * @platform Web,Native
- * @includeExample extension/socket/WebSocket.ts
- * @language zh_CN
- */
- var WebSocket = (function (_super) {
- __extends(WebSocket, _super);
- /**
- * Create an egret.WebSocket object
- * This parameter is reserved for later versions. The connection address and port number are imported in the connect function
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 创建一个 egret.WebSocket 对象
- * 参数为预留参数,现版本暂不处理,连接地址和端口号在 connect 函数中传入
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- function WebSocket(host, port) {
- if (host === void 0) { host = ""; }
- if (port === void 0) { port = 0; }
- var _this = _super.call(this) || this;
- /**
- * @private
- */
- _this._writeMessage = "";
- /**
- * @private
- */
- _this._readMessage = "";
- /**
- * @private
- */
- _this._connected = false;
- /**
- * @private
- */
- _this._connecting = false;
- /**
- * @private
- */
- _this._isReadySend = false;
- /**
- * @private
- */
- _this._bytesWrite = false;
- /**
- * @private
- */
- _this._type = WebSocket.TYPE_STRING;
- _this._connected = false;
- _this._writeMessage = "";
- _this._readMessage = "";
- _this.socket = new egret.ISocket();
- _this.socket.addCallBacks(_this.onConnect, _this.onClose, _this.onSocketData, _this.onError, _this);
- return _this;
- }
- /**
- * Connect the socket to the specified host and port number
- * @param host Name or IP address of the host to be connected
- * @param port Port number to be connected
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 将套接字连接到指定的主机和端口
- * @param host 要连接到的主机的名称或 IP 地址
- * @param port 要连接到的端口号
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.connect = function (host, port) {
- if (!this._connecting && !this._connected) {
- this._connecting = true;
- this.socket.connect(host, port);
- }
- };
- /**
- * 根据提供的url连接
- * @param url 全地址。如ws://echo.websocket.org:80
- */
- WebSocket.prototype.connectByUrl = function (url) {
- if (!this._connecting && !this._connected) {
- this._connecting = true;
- this.socket.connectByUrl(url);
- }
- };
- /**
- * Closesocket
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 关闭套接字
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.close = function () {
- if (this._connected) {
- this.socket.close();
- }
- };
- /**
- * @private
- *
- */
- WebSocket.prototype.onConnect = function () {
- this._connected = true;
- this._connecting = false;
- this.dispatchEventWith(egret.Event.CONNECT);
- };
- /**
- * @private
- *
- */
- WebSocket.prototype.onClose = function () {
- this._connected = false;
- this.dispatchEventWith(egret.Event.CLOSE);
- };
- /**
- * @private
- *
- */
- WebSocket.prototype.onError = function () {
- if (this._connecting) {
- this._connecting = false;
- }
- this.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
- };
- /**
- * @private
- *
- * @param message
- */
- WebSocket.prototype.onSocketData = function (message) {
- if (typeof message == "string") {
- this._readMessage += message;
- }
- else {
- this._readByte._writeUint8Array(new Uint8Array(message));
- }
- egret.ProgressEvent.dispatchProgressEvent(this, egret.ProgressEvent.SOCKET_DATA);
- };
- /**
- * Refresh all data accumulated in the output buffer area of the socket
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 对套接字输出缓冲区中积累的所有数据进行刷新
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.flush = function () {
- if (!this._connected) {
- egret.$warn(3101);
- return;
- }
- if (this._writeMessage) {
- this.socket.send(this._writeMessage);
- this._writeMessage = "";
- }
- if (this._bytesWrite) {
- this.socket.send(this._writeByte.buffer);
- this._bytesWrite = false;
- this._writeByte.clear();
- }
- this._isReadySend = false;
- };
- /**
- * Write data in character string in the socket
- * @param message The character string to be written in the socket
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 将字符串数据写入套接字
- * @param message 要写入套接字的字符串
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.writeUTF = function (message) {
- if (!this._connected) {
- egret.$warn(3101);
- return;
- }
- if (this._type == WebSocket.TYPE_BINARY) {
- this._bytesWrite = true;
- this._writeByte.writeUTF(message);
- }
- else {
- this._writeMessage += message;
- }
- this.flush();
- // return;
- // if (this._isReadySend) {
- // return;
- // }
- // this._isReadySend = true;
- // egret.callLater(this.flush, this);
- };
- /**
- * Read a UTF-8 character string from the socket
- * @returns {string}
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 从套接字读取一个 UTF-8 字符串
- * @returns {string}
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.readUTF = function () {
- var message;
- if (this._type == WebSocket.TYPE_BINARY) {
- this._readByte.position = 0;
- message = this._readByte.readUTF();
- this._readByte.clear();
- }
- else {
- message = this._readMessage;
- this._readMessage = "";
- }
- return message;
- };
- /**
- * Write a series of bytes from the specified byte array. The writing operation starts from the location expressed by offset.
- * If the length parameter is ignored, the default length 0 indicates that data is written from offset in the entire buffer area.
- * If the offset parameter is ignored, data is written in the entire buffer area.
- * @param bytes The ByteArray object where data is read from
- * @param offset Zero-based offset in the ByteArray object. From here start performing data writing
- * @param length Number of bytes to be written Default value 0 indicates data is written in the entire buffer area from the value specified by the offset parameter
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 从指定的字节数组写入一系列字节。写入操作从 offset 指定的位置开始。
- * 如果省略了 length 参数,则默认长度 0 将导致该方法从 offset 开始写入整个缓冲区。
- * 如果还省略了 offset 参数,则写入整个缓冲区。
- * @param bytes 要从中读取数据的 ByteArray 对象
- * @param offset ByteArray 对象中从零开始的偏移量,应由此开始执行数据写入
- * @param length 要写入的字节数。默认值 0 导致从 offset 参数指定的值开始写入整个缓冲区
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.writeBytes = function (bytes, offset, length) {
- if (offset === void 0) { offset = 0; }
- if (length === void 0) { length = 0; }
- if (!this._connected) {
- egret.$warn(3101);
- return;
- }
- if (!this._writeByte) {
- egret.$warn(3102);
- return;
- }
- this._bytesWrite = true;
- this._writeByte.writeBytes(bytes, offset, length);
- this.flush();
- };
- /**
- * Read data byte number specified by the length parameter from the socket. Read these bytes into the specified byte array starting from the location expressed by offset.
- * @param bytes The ByteArray object that data is read into
- * @param offset The offset for data reading starts from this byte array
- * @param length Byte number to be read Default value 0 indicates reading all available data
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 从套接字读取 length 参数指定的数据字节数。从 offset 所表示的位置开始,将这些字节读入指定的字节数组
- * @param bytes 要将数据读入的 ByteArray 对象
- * @param offset 数据读取的偏移量应从该字节数组中开始
- * @param length 要读取的字节数。默认值 0 导致读取所有可用的数据
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.prototype.readBytes = function (bytes, offset, length) {
- if (offset === void 0) { offset = 0; }
- if (length === void 0) { length = 0; }
- if (!this._readByte) {
- egret.$warn(3102);
- return;
- }
- this._readByte.position = 0;
- this._readByte.readBytes(bytes, offset, length);
- this._readByte.clear();
- };
- Object.defineProperty(WebSocket.prototype, "connected", {
- /**
- * Indicates whether the Socket object is connected currently
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 表示此 Socket 对象目前是否已连接
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- get: function () {
- return this._connected;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(WebSocket.prototype, "type", {
- /**
- * Format for sending and receiving data. The default setting is the character string format
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 发送和接收数据的格式,默认是字符串格式
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- get: function () {
- return this._type;
- },
- set: function (value) {
- this._type = value;
- if (value == WebSocket.TYPE_BINARY && !this._writeByte) {
- this._readByte = new egret.ByteArray();
- this._writeByte = new egret.ByteArray();
- }
- },
- enumerable: true,
- configurable: true
- });
- /**
- * Send and receive data in character string format
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 以字符串格式发送和接收数据
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.TYPE_STRING = "webSocketTypeString";
- /**
- * Send and receive data in binary format
- * @version Egret 2.4
- * @platform Web,Native
- * @language en_US
- */
- /**
- * 以二进制格式发送和接收数据
- * @version Egret 2.4
- * @platform Web,Native
- * @language zh_CN
- */
- WebSocket.TYPE_BINARY = "webSocketTypeBinary";
- return WebSocket;
- }(egret.EventDispatcher));
- egret.WebSocket = WebSocket;
- __reflect(WebSocket.prototype, "egret.WebSocket");
- })(egret || (egret = {}));
- //////////////////////////////////////////////////////////////////////////////////////
- //
- // Copyright (c) 2014-present, Egret Technology.
- // All rights reserved.
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above copyright
- // notice, this list of conditions and the following disclaimer in the
- // documentation and/or other materials provided with the distribution.
- // * Neither the name of the Egret nor the
- // names of its contributors may be used to endorse or promote products
- // derived from this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
- // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
- // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- //////////////////////////////////////////////////////////////////////////////////////
- var egret;
- (function (egret) {
- var web;
- (function (web) {
- /**
- * @private
- */
- var HTML5WebSocket = (function () {
- function HTML5WebSocket() {
- this.host = "";
- this.port = 0;
- if (!window["WebSocket"]) {
- egret.$error(3100);
- }
- }
- HTML5WebSocket.prototype.addCallBacks = function (onConnect, onClose, onSocketData, onError, thisObject) {
- this.onConnect = onConnect;
- this.onClose = onClose;
- this.onSocketData = onSocketData;
- this.onError = onError;
- this.thisObject = thisObject;
- };
- HTML5WebSocket.prototype.connect = function (host, port) {
- this.host = host;
- this.port = port;
- var socketServerUrl = "ws://" + this.host + ":" + this.port;
- this.socket = new window["WebSocket"](socketServerUrl);
- this.socket.binaryType = "arraybuffer";
- this._bindEvent();
- };
- HTML5WebSocket.prototype.connectByUrl = function (url) {
- this.socket = new window["WebSocket"](url);
- this.socket.binaryType = "arraybuffer";
- this._bindEvent();
- };
- HTML5WebSocket.prototype._bindEvent = function () {
- var that = this;
- var socket = this.socket;
- socket.onopen = function () {
- if (that.onConnect) {
- that.onConnect.call(that.thisObject);
- }
- };
- socket.onclose = function (e) {
- if (that.onClose) {
- that.onClose.call(that.thisObject);
- }
- };
- socket.onerror = function (e) {
- if (that.onError) {
- that.onError.call(that.thisObject);
- }
- };
- socket.onmessage = function (e) {
- if (that.onSocketData) {
- that.onSocketData.call(that.thisObject, e.data);
- }
- };
- };
- HTML5WebSocket.prototype.send = function (message) {
- this.socket.send(message);
- };
- HTML5WebSocket.prototype.close = function () {
- this.socket.close();
- };
- HTML5WebSocket.prototype.disconnect = function () {
- if (this.socket.disconnect) {
- this.socket.disconnect();
- }
- };
- return HTML5WebSocket;
- }());
- web.HTML5WebSocket = HTML5WebSocket;
- __reflect(HTML5WebSocket.prototype, "egret.web.HTML5WebSocket", ["egret.ISocket"]);
- egret.ISocket = HTML5WebSocket;
- })(web = egret.web || (egret.web = {}));
- })(egret || (egret = {}));
|