Paul 5 years ago
parent
commit
d8f65687a4
3 changed files with 36 additions and 31 deletions
  1. 11
    9
      src/callback.ts
  2. 18
    15
      src/example/main.ts
  3. 7
    7
      src/index.ts

+ 11
- 9
src/callback.ts View File

@@ -1,12 +1,14 @@
1
-export interface ReadyStateCallback {
2
-  onOpen(ev: Event);
3
-  onError(ev: Event);
4
-  onClose(ev: Event);
1
+interface ReadyStateCallback {
2
+  onOpen(ev: Event): void;
3
+  onError(ev: Event): void;
4
+  onClose(ev: Event): void;
5 5
 }
6 6
 
7
-export interface RequestCallback {
8
-  onStart();
9
-  onSuccess(data: string);
10
-  onError(code: number, message: string);
11
-  onEnd();
7
+interface RequestCallback {
8
+  onStart(): void;
9
+  onSuccess(data: string): void;
10
+  onError(code: number, message: string): void;
11
+  onEnd(): void;
12 12
 }
13
+
14
+export { ReadyStateCallback, RequestCallback };

+ 18
- 15
src/example/main.ts View File

@@ -1,19 +1,22 @@
1
-import { Client } from "../index";
1
+import { Client } from '../index';
2 2
 
3
-const client = new Client("ws://127.0.0.1:8081", new class {
4
-  onOpen(ev:Event) {
5
-    console.log("open connection", ev.target)
6
-  }
3
+const url = 'ws://127.0.0.1:8081';
4
+const client = new Client(
5
+  url,
6
+  new (class {
7
+    onOpen(ev: Event) {
8
+      console.log('open connection', ev.target);
9
+    }
7 10
 
8
-  onClose(ev:Event) {
9
-    console.log("connection error", ev)
10
-    console.log(ev)
11
-  }
11
+    onClose(ev: Event) {
12
+      console.log('connection error', ev);
13
+      console.log(ev);
14
+    }
12 15
 
13
-  onError(ev:Event){
14
-    console.log("close connection")
15
-  }
16
-})
16
+    onError(ev: Event) {
17
+      console.log('close connection');
18
+    }
19
+  })(),
20
+);
17 21
 
18
-
19
-console.log(client)
22
+console.log(client);

+ 7
- 7
src/index.ts View File

@@ -36,7 +36,7 @@ export class Client {
36 36
     }
37 37
 
38 38
     let _this = this;
39
-    this.addMessageListener(0, function(data) {
39
+    this.addMessageListener(0, function (data) {
40 40
       let code = _this.getResponseProperty('code');
41 41
       if (typeof code !== 'undefined') {
42 42
         let message = _this.getResponseProperty('message');
@@ -93,7 +93,7 @@ export class Client {
93 93
     let _this = this;
94 94
     let sequence = new Date().getTime();
95 95
     let listener = Utils.crc32(operator) + sequence;
96
-    this.requestCallback[listener] = function(data) {
96
+    this.requestCallback[listener] = function (data) {
97 97
       let code = _this.getResponseProperty('code');
98 98
       if (typeof code !== 'undefined') {
99 99
         let message = _this.getResponseProperty('message');
@@ -206,7 +206,7 @@ export class Client {
206 206
 
207 207
     ws.binaryType = 'blob';
208 208
 
209
-    ws.onopen = function(ev) {
209
+    ws.onopen = function (ev) {
210 210
       _this.reconnectTimes = 0;
211 211
       if (
212 212
         readyStateCallback.hasOwnProperty('onOpen') &&
@@ -216,7 +216,7 @@ export class Client {
216 216
       }
217 217
     };
218 218
 
219
-    ws.onclose = function(ev) {
219
+    ws.onclose = function (ev) {
220 220
       _this.reconnect();
221 221
       if (
222 222
         readyStateCallback.hasOwnProperty('onClose') &&
@@ -226,7 +226,7 @@ export class Client {
226 226
       }
227 227
     };
228 228
 
229
-    ws.onerror = function(ev) {
229
+    ws.onerror = function (ev) {
230 230
       _this.reconnect();
231 231
       if (
232 232
         readyStateCallback.hasOwnProperty('onError') &&
@@ -236,11 +236,11 @@ export class Client {
236 236
       }
237 237
     };
238 238
 
239
-    ws.onmessage = function(ev) {
239
+    ws.onmessage = function (ev) {
240 240
       if (ev.data instanceof Blob) {
241 241
         let reader = new FileReader();
242 242
         reader.readAsArrayBuffer(ev.data);
243
-        reader.onload = function() {
243
+        reader.onload = function () {
244 244
           try {
245 245
             let packet = new Packet().unPack(this.result);
246 246
             let packetLength = packet.headerLength + packet.bodyLength + 20;