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";
|
var CORDOVA_SERVICE_NAME = "SocketsForCordova";
|
||||||
|
|
||||||
function Socket() {
|
function Socket() {
|
||||||
|
this._state = Socket.State.CLOSED;
|
||||||
this.onData = null;
|
this.onData = null;
|
||||||
this.onClose = null;
|
this.onClose = null;
|
||||||
this.onError = null;
|
this.onError = null;
|
||||||
@ -13,6 +14,7 @@ function Socket() {
|
|||||||
Socket.prototype.open = function (host, port, success, error) {
|
Socket.prototype.open = function (host, port, success, error) {
|
||||||
|
|
||||||
var _that = this;
|
var _that = this;
|
||||||
|
|
||||||
function socketEventHandler(event) {
|
function socketEventHandler(event) {
|
||||||
|
|
||||||
var payload = event.payload;
|
var payload = event.payload;
|
||||||
@ -24,6 +26,7 @@ Socket.prototype.open = function (host, port, success, error) {
|
|||||||
switch (payload.type) {
|
switch (payload.type) {
|
||||||
case "Close":
|
case "Close":
|
||||||
//console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
//console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
||||||
|
_that._state = Socket.State.CLOSED;
|
||||||
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
_that.onClose();
|
_that.onClose();
|
||||||
break;
|
break;
|
||||||
@ -41,9 +44,12 @@ Socket.prototype.open = function (host, port, success, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_that._state = Socket.State.OPENING;
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function () {
|
function () {
|
||||||
//console.debug("SocketsForCordova: Socket successfully opened.");
|
//console.debug("SocketsForCordova: Socket successfully opened.");
|
||||||
|
_that._state = Socket.State.OPENED;
|
||||||
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
if (success)
|
if (success)
|
||||||
success();
|
success();
|
||||||
@ -122,6 +128,19 @@ Socket.prototype.close = function () {
|
|||||||
[ this.socketKey ]);
|
[ 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) {
|
Socket.dispatchEvent = function (event) {
|
||||||
var eventReceive = document.createEvent('Events');
|
var eventReceive = document.createEvent('Events');
|
||||||
eventReceive.initEvent(SOCKET_EVENT, true, true);
|
eventReceive.initEvent(SOCKET_EVENT, true, true);
|
||||||
@ -136,6 +155,7 @@ var guid = (function() {
|
|||||||
.toString(16)
|
.toString(16)
|
||||||
.substring(1);
|
.substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return function () {
|
return function () {
|
||||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
||||||
s4() + '-' + s4() + s4() + s4();
|
s4() + '-' + s4() + s4() + s4();
|
||||||
|
Loading…
Reference in New Issue
Block a user