forked from ipald/nativescript-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketio.android.ts
174 lines (150 loc) · 5.44 KB
/
socketio.android.ts
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
import { Common, SocketOptions, enableDebug, disableDebug, debug } from "./socketio.common";
import { serialize, deserialize } from "./helpers";
const _Emitter = io.socket.emitter.Emitter;
const _IO = io.socket.client.IO;
const _Socket = io.socket.client.Socket;
const _Ack = io.socket.client.Ack;
export { SocketOptions, enableDebug, disableDebug };
const SOCKET_CLASS : string = 'io.socket.client.Socket';
export class SocketIO extends Common {
private android: io.socket.client.Socket;
constructor(uri: string, options: SocketOptions = {}) {
super();
let _options = new _IO.Options();
if (options.query) {
if (typeof options.query === 'string') {
_options.query = options.query;
} else {
_options.query = Object.keys(options.query).map(function(key){
return encodeURIComponent(key) + '=' + encodeURIComponent(options.query[key]);
}).join('&');
}
}
if (options.forceWebsockets) {
_options.transports = Array.create(java.lang.String, 1);
_options.transports[0] = 'websocket';
}
// if ('secure' in options) {
// _options.secure = !!options.secure;
// }
if (options.android) {
Object.keys(options.android).forEach(function(prop) {
_options[prop] = options.android[prop];
});
}
this.android = _IO.socket(uri, _options);
}
get connected(): boolean {
return this.android && this.android.connected();
}
connect() {
this.android.connect();
}
disconnect() {
this.android.disconnect();
}
on(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) {
const listener = function(args) {
let payload = Array.prototype.slice.call(args);
let ack = payload.pop();
if (typeof ack === 'undefined') {
ack = null;
} else if (typeof ack === 'object' && ack && !(ack.getClass().getName().indexOf(SOCKET_CLASS) === 0 && ack.call)) {
payload.push(ack);
ack = null;
}
payload = payload.map(deserialize);
debug('on', event, payload, ack ? 'ack' : '');
if (ack) {
let _ack = function(...args) {
debug('on', event, 'ack', args);
args = args.map(serialize);
ack.call(args);
};
payload.push(_ack);
}
callback.apply(null, payload);
};
const nativeListener = new _Emitter.Listener({
call: listener,
});
this._listeners.set(callback, nativeListener );
this.android.on(event, nativeListener);
return this;
}
once(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) {
const listener = function(args) {
let payload = Array.prototype.slice.call(args);
let ack = payload.pop();
if (typeof ack === 'undefined') {
ack = null;
} else if (typeof ack === 'object' && ack && !(ack.getClass().getName().indexOf(SOCKET_CLASS) === 0 && ack.call)) {
payload.push(ack);
ack = null;
}
payload = payload.map(deserialize);
debug('once', event, payload, ack ? 'ack' : '');
if (ack) {
let _ack = function(...args) {
debug('once', event, 'ack', args);
args = args.map(serialize);
ack.call(args);
};
payload.push(_ack);
}
callback.apply(null, payload);
};
const nativeListener = new _Emitter.Listener({
call: listener,
});
this._listeners.set(callback, nativeListener);
this.android.once(event, nativeListener);
return this;
}
off(event: string, callback?: Function) {
debug('off', event, callback);
if (callback) {
let listener = this._listeners.get(callback);
if (listener) {
this.android.off(event, listener);
this._listeners.delete(callback);
}
} else {
this.android.off(event);
}
return this;
}
emit(event: string, ...payload: Array<any> /*, ack?: Function */) {
let ack = payload.pop();
if (typeof ack === 'undefined') {
ack = null;
} else if (typeof ack !== 'function') {
payload.push(ack);
ack = null;
}
debug('emit', event, payload, ack ? 'ack' : '');
payload = payload.map(serialize);
if (ack) {
let _ack = function(args) {
args = Array.prototype.slice.call(args).map(deserialize);
debug('emit', event, 'ack', args);
ack.apply(null, args);
};
const _nativeAck = new _Ack({
call: _ack,
});
payload.push(_nativeAck);
}
this.android.emit(event, payload);
return this;
}
removeAllListeners(): this {
this.android.off();
return this;
}
}
export function connect(uri: string, options?: SocketOptions): SocketIO {
let socketio = new SocketIO(uri, options || {});
socketio.connect();
return socketio;
}