Proposal impleementation of state property on Socket prototype
This commit is contained in:
parent
88bc09fb83
commit
df1d4f8272
50
socket.js
50
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;
|
||||
@ -21,9 +23,10 @@ Socket.prototype.open = function (host, port, success, error) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch(payload.type) {
|
||||
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,14 +44,17 @@ Socket.prototype.open = function (host, port, success, error) {
|
||||
}
|
||||
}
|
||||
|
||||
_that._state = Socket.State.OPENING;
|
||||
|
||||
exec(
|
||||
function() {
|
||||
function () {
|
||||
//console.debug("SocketsForCordova: Socket successfully opened.");
|
||||
_that._state = Socket.State.OPENED;
|
||||
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||
if (success)
|
||||
success();
|
||||
},
|
||||
function(errorMessage) {
|
||||
function (errorMessage) {
|
||||
//console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage);
|
||||
if (error)
|
||||
error(errorMessage);
|
||||
@ -65,12 +71,12 @@ Socket.prototype.write = function (data, success, error) {
|
||||
: data;
|
||||
|
||||
exec(
|
||||
function() {
|
||||
function () {
|
||||
//console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length);
|
||||
if (success)
|
||||
success();
|
||||
},
|
||||
function(errorMessage) {
|
||||
function (errorMessage) {
|
||||
//console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage);
|
||||
if (error)
|
||||
error(errorMessage);
|
||||
@ -80,7 +86,7 @@ Socket.prototype.write = function (data, success, error) {
|
||||
[ this.socketKey, dataToWrite ]);
|
||||
};
|
||||
|
||||
Socket._copyToArray = function(array) {
|
||||
Socket._copyToArray = function (array) {
|
||||
var outputArray = new Array(array.length);
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
outputArray[i] = array[i];
|
||||
@ -90,12 +96,12 @@ Socket._copyToArray = function(array) {
|
||||
|
||||
Socket.prototype.shutdownWrite = function (success, error) {
|
||||
exec(
|
||||
function() {
|
||||
function () {
|
||||
//console.debug("SocketsForCordova: Shutdown write successfully called.");
|
||||
if (success)
|
||||
success();
|
||||
},
|
||||
function(errorMessage) {
|
||||
function (errorMessage) {
|
||||
//console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage);
|
||||
if (error)
|
||||
error(errorMessage);
|
||||
@ -107,12 +113,12 @@ Socket.prototype.shutdownWrite = function (success, error) {
|
||||
|
||||
Socket.prototype.close = function () {
|
||||
exec(
|
||||
function() {
|
||||
function () {
|
||||
//console.debug("SocketsForCordova: Close successfully called.");
|
||||
if (success)
|
||||
success();
|
||||
},
|
||||
function(errorMessage) {
|
||||
function (errorMessage) {
|
||||
//console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage);
|
||||
if (error)
|
||||
error(errorMessage);
|
||||
@ -122,7 +128,20 @@ Socket.prototype.close = function () {
|
||||
[ this.socketKey ]);
|
||||
};
|
||||
|
||||
Socket.dispatchEvent = function(event) {
|
||||
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);
|
||||
eventReceive.payload = event;
|
||||
@ -130,13 +149,14 @@ Socket.dispatchEvent = function(event) {
|
||||
document.dispatchEvent(eventReceive);
|
||||
};
|
||||
|
||||
var guid = (function() {
|
||||
var guid = (function () {
|
||||
function s4() {
|
||||
return Math.floor((1 + Math.random()) * 0x10000)
|
||||
.toString(16)
|
||||
.substring(1);
|
||||
}
|
||||
return function() {
|
||||
|
||||
return function () {
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
||||
s4() + '-' + s4() + s4() + s4();
|
||||
};
|
||||
@ -144,10 +164,10 @@ var guid = (function() {
|
||||
|
||||
// Register event dispatcher for Windows Phone
|
||||
if (navigator.userAgent.match(/iemobile/i)) {
|
||||
window.document.addEventListener("deviceready", function() {
|
||||
window.document.addEventListener("deviceready", function () {
|
||||
exec(
|
||||
Socket.dispatchEvent,
|
||||
function(errorMessage) {
|
||||
function (errorMessage) {
|
||||
console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage);
|
||||
},
|
||||
CORDOVA_SERVICE_NAME,
|
||||
|
Loading…
Reference in New Issue
Block a user