This repository was archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsocketio.ios.ts
More file actions
164 lines (138 loc) · 5.01 KB
/
socketio.ios.ts
File metadata and controls
164 lines (138 loc) · 5.01 KB
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
'use strict';
import { Common, SocketOptions, enableDebug, disableDebug, debug } from "./socketio.common";
import { serialize, deserialize } from "./helpers";
SocketIOClient; // FIX: unrecognized class issue
SocketAckEmitter; // FIX: unrecognized class issue
OnAckCallback; // FIX: unrecognized class issue
SocketEngine; // FIX: unrecognized class issue
export { SocketOptions, enableDebug, disableDebug };
const NAMESPACE_REGEXP : RegExp = /^https?\:\/\/[^\/]*(\/.*)$/i;
export class SocketIO extends Common {
private ios: SocketIOClient;
constructor(uri: string, options: SocketOptions = {}) {
super();
let _options : any = {};
if (NAMESPACE_REGEXP.test(uri)) {
_options.nsp = uri.match(NAMESPACE_REGEXP)[1] || '/';
} else {
_options.nsp = '/';
}
if (options.query) {
_options.connectParams = {};
if (typeof options.query === 'string') {
options.query.split('&').forEach(function(pair: any){
pair = pair.split('=').map(decodeURIComponent);
_options.connectParams[pair[0]] = pair[1];
});
} else {
Object.keys(options.query).forEach(function(key){
_options.connectParams[key] = String(options.query[key]);
});
}
}
if (options.forceWebsockets) {
_options.forceWebsockets = true;
}
// if ('secure' in options) {
// _options.secure = !!options.secure;
// }
if (options.ios) {
Object.keys(options.ios).forEach(function(prop) {
_options[prop] = options.ios[prop];
});
}
this.ios = SocketIOClient.alloc().initWithSocketURLConfig(NSURL.URLWithString(uri), _options);
}
get connected(): boolean {
return this.ios && this.ios.engine.connected;
}
connect() {
this.ios.connect();
}
disconnect() {
this.ios.disconnect();
}
on(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) {
let listener = function(data: Array<any>, ack: any) {
let payload = deserialize(data);
debug('on', event, payload, ack && ack.expected ? 'ack' : '');
if (ack && ack.expected) {
let _ack = function(...args) {
debug('on', event, 'ack', args);
args = args.map(serialize)
ack.with(args);
};
payload.push(_ack);
}
callback.apply(null, payload);
};
// @ts-ignore
let listenerId = this.ios.onCallback(event, listener);
this._listeners.set(callback, listenerId);
return this;
}
once(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) {
let listener = function(data: Array<any>, ack: any) {
let payload = deserialize(data);
debug('once', event, payload, ack && ack.expected ? 'ack' : '');
if (ack && ack.expected) {
let _ack = function(...args) {
debug('once', event, 'ack', args);
args = args.map(serialize)
ack.with(args);
};
payload.push(_ack);
}
callback.apply(null, payload);
};
// @ts-ignore
let listenerId = this.ios.onceCallback(event, listener);
this._listeners.set(callback, listenerId);
return this;
}
off(event: string, callback?: Function) {
debug('off', event, callback);
if (callback) {
let listenerId = this._listeners.get(callback);
if (listenerId) {
this.ios.offWithId(listenerId);
this._listeners.delete(callback);
}
} else {
this.ios.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' : '');
// @ts-ignore
const _payload : NSArray<any> = payload.map(serialize);
if (ack) {
let _ack = function(args) {
args = deserialize(args);
debug('emit', event, 'ack', args);
ack.apply(null, args);
};
this.ios.emitWithAckWith(event, _payload).timingOutAfterCallback(0, _ack);
} else {
this.ios.emitWith(event, _payload);
}
return this;
}
removeAllListeners(): this {
this.ios.removeAllHandlers();
return this;
}
}
export function connect(uri: string, options?: SocketOptions): SocketIO {
let socketio = new SocketIO(uri, options || {});
socketio.connect();
return socketio;
}