-
Notifications
You must be signed in to change notification settings - Fork 128
/
socket.js
213 lines (175 loc) · 5.64 KB
/
socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Copyright (c) 2015, Blocshop s.r.o.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the Blocshop s.r.o.. The name of the
* Blocshop s.r.o. may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
var exec = require('cordova/exec');
var SOCKET_EVENT = "SOCKET_EVENT";
var CORDOVA_SERVICE_NAME = "SocketsForCordova";
Socket.State = {};
Socket.State[Socket.State.CLOSED = 0] = "CLOSED";
Socket.State[Socket.State.OPENING = 1] = "OPENING";
Socket.State[Socket.State.OPENED = 2] = "OPENED";
Socket.State[Socket.State.CLOSING = 3] = "CLOSING";
function Socket() {
this._state = Socket.State.CLOSED;
this.onData = null;
this.onClose = null;
this.onError = null;
this.socketKey = guid();
}
Socket.prototype.open = function (host, port, success, error) {
success = success || function() { };
error = error || function() { };
if (!this._ensureState(Socket.State.CLOSED, error)) {
return;
}
var _that = this;
function socketEventHandler(event) {
var payload = event.payload;
if (payload.socketKey !== _that.socketKey) {
return;
}
switch (payload.type) {
case "Close":
_that._state = Socket.State.CLOSED;
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
_that.onClose(payload.hasError);
break;
case "DataReceived":
_that.onData(new Uint8Array(payload.data));
break;
case "Error":
_that.onError(payload.errorMessage);
break;
default:
console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey);
break;
}
}
_that._state = Socket.State.OPENING;
exec(
function () {
_that._state = Socket.State.OPENED;
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
success();
},
function(errorMessage) {
_that._state = Socket.State.CLOSED;
error(errorMessage);
},
CORDOVA_SERVICE_NAME,
"open",
[ this.socketKey, host, port ]);
};
Socket.prototype.write = function (data, success, error) {
success = success || function() { };
error = error || function() { };
if (!this._ensureState(Socket.State.OPENED, error)) {
return;
}
var dataToWrite = data instanceof Uint8Array
? Socket._copyToArray(data)
: data;
exec(
success,
error,
CORDOVA_SERVICE_NAME,
"write",
[ this.socketKey, dataToWrite ]);
};
Socket.prototype.shutdownWrite = function (success, error) {
success = success || function() { };
error = error || function() { };
if (!this._ensureState(Socket.State.OPENED, error)) {
return;
}
exec(
success,
error,
CORDOVA_SERVICE_NAME,
"shutdownWrite",
[ this.socketKey ]);
};
Socket.prototype.close = function (success, error) {
success = success || function() { };
error = error || function() { };
if (!this._ensureState(Socket.State.OPENED, error)) {
return;
}
this._state = Socket.State.CLOSING;
exec(
success,
error,
CORDOVA_SERVICE_NAME,
"close",
[ this.socketKey ]);
};
Object.defineProperty(Socket.prototype, "state", {
get: function () {
return this._state;
},
enumerable: true,
configurable: true
});
Socket.prototype._ensureState = function(requiredState, errorCallback) {
var state = this._state;
if (state != requiredState) {
window.setTimeout(function() {
errorCallback("Invalid operation for this socket state: " + Socket.State[state]);
});
return false;
}
else {
return true;
}
};
Socket.dispatchEvent = function (event) {
var eventReceive = document.createEvent('Events');
eventReceive.initEvent(SOCKET_EVENT, true, true);
eventReceive.payload = event;
document.dispatchEvent(eventReceive);
};
Socket._copyToArray = function (array) {
var outputArray = new Array(array.length);
for (var i = 0; i < array.length; i++) {
outputArray[i] = array[i];
}
return outputArray;
};
var guid = (function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function () {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
})();
// Register event dispatcher for Windows Phone
if (navigator.userAgent.match(/iemobile/i)) {
window.document.addEventListener("deviceready", function () {
exec(
Socket.dispatchEvent,
function (errorMessage) {
console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage);
},
CORDOVA_SERVICE_NAME,
"registerWPEventDispatcher",
[ ]);
});
}
module.exports = Socket;