Proposal impleementation of state property on Socket prototype
This commit is contained in:
parent
88bc09fb83
commit
df1d4f8272
20
socket.js
20
socket.js
@ -4,6 +4,7 @@ var SOCKET_EVENT = "SOCKET_EVENT";
|
||||
var CORDOVA_SERVICE_NAME = "SocketsForCordova";
|
||||
|
||||
function Socket() {
|
||||
this._state = Socket.State.CLOSED;
|
||||
this.onData = null;
|
||||
this.onClose = null;
|
||||
this.onError = null;
|
||||
@ -13,6 +14,7 @@ function Socket() {
|
||||
Socket.prototype.open = function (host, port, success, error) {
|
||||
|
||||
var _that = this;
|
||||
|
||||
function socketEventHandler(event) {
|
||||
|
||||
var payload = event.payload;
|
||||
@ -24,6 +26,7 @@ Socket.prototype.open = function (host, port, success, error) {
|
||||
switch (payload.type) {
|
||||
case "Close":
|
||||
//console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
||||
_that._state = Socket.State.CLOSED;
|
||||
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
||||
_that.onClose();
|
||||
break;
|
||||
@ -41,9 +44,12 @@ Socket.prototype.open = function (host, port, success, error) {
|
||||
}
|
||||
}
|
||||
|
||||
_that._state = Socket.State.OPENING;
|
||||
|
||||
exec(
|
||||
function () {
|
||||
//console.debug("SocketsForCordova: Socket successfully opened.");
|
||||
_that._state = Socket.State.OPENED;
|
||||
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||
if (success)
|
||||
success();
|
||||
@ -122,6 +128,19 @@ Socket.prototype.close = function () {
|
||||
[ this.socketKey ]);
|
||||
};
|
||||
|
||||
Object.defineProperty(Socket.prototype, "state", {
|
||||
get: function () {
|
||||
return this._state;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Socket.State = {};
|
||||
Socket.State[Socket.State.CLOSED = 0] = "CLOSED";
|
||||
Socket.State[Socket.State.OPENING = 1] = "OPENING";
|
||||
Socket.State[Socket.State.OPENED = 2] = "OPENED";
|
||||
|
||||
Socket.dispatchEvent = function (event) {
|
||||
var eventReceive = document.createEvent('Events');
|
||||
eventReceive.initEvent(SOCKET_EVENT, true, true);
|
||||
@ -136,6 +155,7 @@ var guid = (function() {
|
||||
.toString(16)
|
||||
.substring(1);
|
||||
}
|
||||
|
||||
return function () {
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
||||
s4() + '-' + s4() + s4() + s4();
|
||||
|
Loading…
Reference in New Issue
Block a user