Browse Source

fix style

Paul 5 years ago
parent
commit
df9231f1f0
3 changed files with 23 additions and 12 deletions
  1. 2
    2
      src/callback.ts
  2. 17
    3
      src/example/main.ts
  3. 4
    7
      src/index.ts

+ 2
- 2
src/callback.ts View File

1
-export interface readyStateCallback {
1
+export interface ReadyStateCallback {
2
   onOpen(ev: Event);
2
   onOpen(ev: Event);
3
   onError(ev: Event);
3
   onError(ev: Event);
4
   onClose(ev: Event);
4
   onClose(ev: Event);
5
 }
5
 }
6
 
6
 
7
-export interface callback {
7
+export interface RequestCallback {
8
   onStart();
8
   onStart();
9
   onSuccess(data: string);
9
   onSuccess(data: string);
10
   onError(code: number, message: string);
10
   onError(code: number, message: string);

+ 17
- 3
src/example/main.ts View File

1
 import { Client } from "../index";
1
 import { Client } from "../index";
2
 
2
 
3
-const client = new Client("ws://127.0.0.1:8081", null)
4
-console.log(client.setRequestProperty("name", "stri"))
5
-console.log(client.getRequestProperty("name"))
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
+  }
7
+
8
+  onClose(ev:Event) {
9
+    console.log("connection error", ev)
10
+    console.log(ev)
11
+  }
12
+
13
+  onError(ev:Event){
14
+    console.log("close connection")
15
+  }
16
+})
17
+
18
+
19
+console.log(client)

+ 4
- 7
src/index.ts View File

1
 import * as constant from './constant';
1
 import * as constant from './constant';
2
-import { readyStateCallback, callback } from './callback';
2
+import { ReadyStateCallback, RequestCallback } from './callback';
3
 import { Packet } from './packet';
3
 import { Packet } from './packet';
4
 import { Utils } from './utils';
4
 import { Utils } from './utils';
5
 
5
 
7
  * Client ws client, 单例模式, 负责维护连接
7
  * Client ws client, 单例模式, 负责维护连接
8
  */
8
  */
9
 export class Client {
9
 export class Client {
10
-  private requestCallback: callback;
10
+  private requestCallback: RequestCallback;
11
   private requestHeader: string;
11
   private requestHeader: string;
12
   private responseHeader: string;
12
   private responseHeader: string;
13
   private maxPayload: number;
13
   private maxPayload: number;
15
   private reconnectTimes: number;
15
   private reconnectTimes: number;
16
   private reconnectLock: boolean;
16
   private reconnectLock: boolean;
17
   private socket: WebSocket;
17
   private socket: WebSocket;
18
-  private readyStateCallback: readyStateCallback;
18
+  private readyStateCallback: ReadyStateCallback;
19
 
19
 
20
-  constructor(url: string, readyStateCallback: readyStateCallback) {
20
+  constructor(url: string, readyStateCallback: ReadyStateCallback) {
21
     this.maxPayload = constant.MAX_PAYLOAD;
21
     this.maxPayload = constant.MAX_PAYLOAD;
22
     this.url = url;
22
     this.url = url;
23
     this.readyStateCallback = readyStateCallback;
23
     this.readyStateCallback = readyStateCallback;
207
     ws.binaryType = 'blob';
207
     ws.binaryType = 'blob';
208
 
208
 
209
     ws.onopen = function(ev) {
209
     ws.onopen = function(ev) {
210
-      console.info('websocket connected');
211
       _this.reconnectTimes = 0;
210
       _this.reconnectTimes = 0;
212
       if (
211
       if (
213
         readyStateCallback.hasOwnProperty('onOpen') &&
212
         readyStateCallback.hasOwnProperty('onOpen') &&
218
     };
217
     };
219
 
218
 
220
     ws.onclose = function(ev) {
219
     ws.onclose = function(ev) {
221
-      console.info('websocket disconnected');
222
       _this.reconnect();
220
       _this.reconnect();
223
       if (
221
       if (
224
         readyStateCallback.hasOwnProperty('onClose') &&
222
         readyStateCallback.hasOwnProperty('onClose') &&
229
     };
227
     };
230
 
228
 
231
     ws.onerror = function(ev) {
229
     ws.onerror = function(ev) {
232
-      console.info('websocket error disconnected');
233
       _this.reconnect();
230
       _this.reconnect();
234
       if (
231
       if (
235
         readyStateCallback.hasOwnProperty('onError') &&
232
         readyStateCallback.hasOwnProperty('onError') &&