|
@@ -71,54 +71,30 @@ class Client {
|
71
|
71
|
* @param {*} param
|
72
|
72
|
* @param {*} callback 仅此次有效的callback
|
73
|
73
|
*/
|
74
|
|
- asyncSend(operator, param, callback) {
|
|
74
|
+ asyncSend(operator: string, param: any, callback: RequestCallback) {
|
75
|
75
|
console.info('websocket send data', operator, this.requestHeader, param);
|
76
|
76
|
|
77
|
|
- if (typeof callback !== 'object') {
|
78
|
|
- throw new Error('callback must be an object');
|
79
|
|
- }
|
80
|
|
-
|
81
|
77
|
if (this.socket.readyState !== this.socket.OPEN) {
|
82
|
78
|
throw new Error('asyncSend: connection refuse');
|
83
|
79
|
}
|
84
|
80
|
|
85
|
|
- if (
|
86
|
|
- callback.hasOwnProperty('onStart') &&
|
87
|
|
- typeof callback.onStart === 'function'
|
88
|
|
- ) {
|
89
|
|
- callback.onStart();
|
90
|
|
- }
|
|
81
|
+ callback.onStart();
|
91
|
82
|
|
92
|
|
- let sequence = new Date().getTime();
|
93
|
|
- let listener = Utils.crc32(operator) + sequence;
|
94
|
|
- this.requestCallback[listener] = (data) => {
|
95
|
|
- let code = this.getResponseProperty('code');
|
96
|
|
- if (typeof code !== 'undefined') {
|
97
|
|
- let message = this.getResponseProperty('message');
|
98
|
|
- if (
|
99
|
|
- callback.hasOwnProperty('onError') &&
|
100
|
|
- typeof callback.onError === 'function'
|
101
|
|
- ) {
|
102
|
|
- callback.onError(code, message);
|
103
|
|
- }
|
|
83
|
+ const sequence = new Date().getTime();
|
|
84
|
+ const listener = Utils.crc32(operator) + sequence;
|
|
85
|
+ this.listeners.set(listener, (data: string) => {
|
|
86
|
+ const code = this.getResponseProperty('code');
|
|
87
|
+ if (code !== '') {
|
|
88
|
+ const message = this.getResponseProperty('message');
|
|
89
|
+ callback.onError(Number(code), message);
|
104
|
90
|
} else {
|
105
|
|
- if (
|
106
|
|
- callback.hasOwnProperty('onSuccess') &&
|
107
|
|
- typeof callback.onSuccess === 'function'
|
108
|
|
- ) {
|
109
|
|
- callback.onSuccess(data);
|
110
|
|
- }
|
|
91
|
+ callback.onSuccess(data);
|
111
|
92
|
}
|
112
|
93
|
|
113
|
|
- if (
|
114
|
|
- callback.hasOwnProperty('onEnd') &&
|
115
|
|
- typeof callback.onEnd === 'function'
|
116
|
|
- ) {
|
117
|
|
- callback.onEnd();
|
118
|
|
- }
|
|
94
|
+ callback.onEnd();
|
119
|
95
|
|
120
|
|
- delete this.requestCallback[listener];
|
121
|
|
- };
|
|
96
|
+ delete this.listeners[listener];
|
|
97
|
+ });
|
122
|
98
|
|
123
|
99
|
const p = new Packet();
|
124
|
100
|
this.send(
|
|
@@ -132,7 +108,7 @@ class Client {
|
132
|
108
|
}
|
133
|
109
|
|
134
|
110
|
// 同步请求服务端数据
|
135
|
|
- async syncSend(operator, param, callback) {
|
|
111
|
+ async syncSend(operator: string, param: any, callback: RequestCallback) {
|
136
|
112
|
await this.asyncSend(operator, param, callback);
|
137
|
113
|
}
|
138
|
114
|
|