interface.uts 14.7 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8
export interface Uni {
  /**
    * ConnectSocket()
    * @description 
    * 创建一个 WebSocket 连接。
    * @param {ConnectSocketOptions} options
    * @return {SocketTask}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#connectsocket
DCloud-yyl's avatar
DCloud-yyl 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    * @example
     ```typescript
        uni.connectSocket({
            url: "ws://192.168.12.106:8080/ws",
            complete: (e) => {
              console.log("socket :", e);
            }
          });
     ```
    */
  connectSocket(options: ConnectSocketOptions): SocketTask;
  /**
    * OnSocketOpen()
    * @description 
    * 监听WebSocket连接打开事件。
    * @param {OnSocketOpenCallback} options
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#onsocketopen
DCloud-yyl's avatar
DCloud-yyl 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    * @example
     ```typescript
      uni.onSocketOpen(function (res) {
        console.log('WebSocket连接已打开!');
      });
     ```
    */
  onSocketOpen(options: OnSocketOpenCallback): void;

  /**
    * OnSocketError()
    * @description 
    * 下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。
    * @param {OnSocketErrorCallback} callback
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#onsocketerror
DCloud-yyl's avatar
DCloud-yyl 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    * @example
     ```typescript
      uni.onSocketError(function (res) {
        console.log('WebSocket连接打开失败,请检查!');
      });
     ```
    */
  onSocketError(callback: OnSocketErrorCallback): void;

  /**
    * SendSocketMessage()
    * @description 
    * 通过 WebSocket 连接发送数据,需要先 uni.connectSocket,并在 uni.onSocketOpen 回调之后才能发送。
    * @param {SendSocketMessageOptions} options
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#sendsocketmessage
DCloud-yyl's avatar
DCloud-yyl 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    * @example
     ```typescript
      uni.sendSocketMessage({
        data: msg
      });
     ```
    */
  sendSocketMessage(options: SendSocketMessageOptions): void;
  /**
    * OnSocketMessage()
    * @description 
    * 监听WebSocket接受到服务器的消息事件。
    * @param {OnSocketMessageCallback} callback
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#onsocketmessage
DCloud-yyl's avatar
DCloud-yyl 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    * @example
     ```typescript
      uni.onSocketMessage(function (res) {
        console.log('收到服务器内容:' + res.data);
      });
     ```
    */
  onSocketMessage(callback: OnSocketMessageCallback): void;
  /**
    * CloseSocket()
    * @description 
    * 关闭 WebSocket 连接。
    * @param {CloseSocketOptions} options
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#closesocket
DCloud-yyl's avatar
DCloud-yyl 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
    * @example
     ```typescript
      uni.closeSocket();
     ```
    */
  closeSocket(options: CloseSocketOptions): void;
  /**
    * OnSocketClose()
    * @description 
    * 监听WebSocket关闭。
    * @param {OnSocketCloseCallback} callback
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/websocket.html#onsocketclose
DCloud-yyl's avatar
DCloud-yyl 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    * @example
     ```typescript
      uni.onSocketClose(function (res) {
        console.log('WebSocket 已关闭!');
      });
     ```
    */
  onSocketClose(callback: OnSocketCloseCallback): void;
}

export type ConnectSocket = (options: ConnectSocketOptions) => SocketTask;
export type ConnectSocketSuccess = {
  errMsg: string
};
type ConnectSocketSuccessCallback = (result: ConnectSocketSuccess) => void;
/**
 * 错误码
 * - 600009 URL格式不合法
 */
export type ConnectSocketErrorCode = 600009;

/**
 * 连接调用失败的错误回调参数
 */
export interface ConnectSocketFail extends IUniError { 
	errCode: ConnectSocketErrorCode;
};

type ConnectSocketFailCallback = (result: ConnectSocketFail) => void;
type ConnectSocketComplete = any;
type ConnectSocketCompleteCallback = (result: ConnectSocketComplete) => void;
export type ConnectSocketOptions = {
  /**
   * 开发者服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名
   */
  url: string,
  /**
   * HTTP 请求 Header,header 中不能设置 Referer
   * @defaultValue null
   */
DCloud-yyl's avatar
DCloud-yyl 已提交
275
  header?: UTSJSONObject | null,
DCloud-yyl's avatar
DCloud-yyl 已提交
276 277 278 279
  /**
   * 子协议数组
   * @defaultValue null
   */
DCloud-yyl's avatar
DCloud-yyl 已提交
280
  protocols?: (string[]) | null,
DCloud-yyl's avatar
DCloud-yyl 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  /**
   * 接口调用成功的回调函数
   * @defaultValue null
   */
  success?: ConnectSocketSuccessCallback | null,
  /**
   * 接口调用失败的回调函数
   * @defaultValue null
   */
  fail?: ConnectSocketFailCallback | null,
  /**
   * 接口调用结束的回调函数(调用成功、失败都会执行)
   * @defaultValue null
   */
  complete?: ConnectSocketCompleteCallback | null
};
export type GeneralCallbackResult = {
  /**
   * 错误信息
   */
  errMsg: string
};
DCloud-yyl's avatar
DCloud-yyl 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315
/**
 * 错误码
 * - 10001 发送数据超限,发送队列不能超过16M大小。
 * - 10002 websocket未连接
 * - 602001 websocket系统错误
 */
export type SendSocketMessageErrorCode = 10001 | 10002 | 602001;
/**
 * 发送失败的错误回调参数
 */
export interface SendSocketMessageFail extends IUniError { 
	errCode: SendSocketMessageErrorCode;
};
DCloud-yyl's avatar
DCloud-yyl 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
export type SendSocketMessageOptions = {
  /**
   * 需要发送的内容
   * @type string | ArrayBuffer
   * @type {SocketDataOptions}
   */
  data: any,
  /**
   * 接口调用成功的回调函数
   * @defaultValue null
   */
  success?: ((result: GeneralCallbackResult) => void) | null,
  /**
   * 接口调用失败的回调函数
   * @defaultValue null
   */
DCloud-yyl's avatar
DCloud-yyl 已提交
332
  fail?: ((result: SendSocketMessageFail) => void) | null,
DCloud-yyl's avatar
DCloud-yyl 已提交
333 334 335 336
  /**
   * 接口调用结束的回调函数(调用成功、失败都会执行)
   * @defaultValue null
   */
DCloud-yyl's avatar
DCloud-yyl 已提交
337
  complete?: ((result: any) => void) | null
DCloud-yyl's avatar
DCloud-yyl 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
};
export type CloseSocketOptions = {
  /**
   * 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。如果这个参数没有被指定,默认的取值是1000 (表示正常连接关闭)
   * @defaultValue 1000
   */
  code?: number | null,
  /**
   * 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于123字节的UTF-8 文本(不是字符)
   * @defaultValue ""
   */
  reason?: string | null,
  /**
   * 接口调用成功的回调函数
   * @defaultValue null
   */
  success?: ((result: GeneralCallbackResult) => void) | null,
  /**
   * 接口调用失败的回调函数
   * @defaultValue null
   */
  fail?: ((result: GeneralCallbackResult) => void) | null,
  /**
   * 接口调用结束的回调函数(调用成功、失败都会执行)
   * @defaultValue null
   */
  complete?: ((result: GeneralCallbackResult) => void) | null
};
export type OnSocketOpenCallbackResult = {
  /**
   * 连接成功的 HTTP 响应 Header
   */
  header: any
};
export type OnSocketMessageCallbackResult = {
  /**
   * 服务器返回的消息
   * @type {SocketDataOptions}
   */
  data: any
};
export interface SocketTask {
  /**
    * send()
    * @description 
    * 通过 WebSocket 连接发送数据
    * @param {SendSocketMessageOptions} options
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/socket-task.html#sockettask-send
DCloud-yyl's avatar
DCloud-yyl 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418
    * @example
     ```typescript
      task.send({data:"halo"});
     ```
    */
  send(options: SendSocketMessageOptions): void;
  /**
    * close()
    * @description 
    * 关闭 WebSocket 连接
    * @param {CloseSocketOptions} options
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/socket-task.html#sockettask-close
DCloud-yyl's avatar
DCloud-yyl 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450
    * @example
     ```typescript
      task.close();
     ```
    */
  close(options: CloseSocketOptions): void;
  /**
    * onOpen()
    * @description 
    * 监听 WebSocket 连接打开事件
    * @param {OnSocketOpenCallbackResult} options
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/socket-task.html#sockettask-onopen
DCloud-yyl's avatar
DCloud-yyl 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482
    * @example
     ```typescript
      task.onOpen((res) => {})
     ```
    */
  onOpen(callback: (result: OnSocketOpenCallbackResult) => void): void;
  /**
    * onClose()
    * @description 
    * 监听 WebSocket 连接关闭事件
    * @param {(result : any) => void} callback
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/socket-task.html#sockettask-onclose
DCloud-yyl's avatar
DCloud-yyl 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515
    * @example
     ```typescript
      task.onClose((res) => {
      })
     ```
    */
  onClose(callback: (result: any) => void): void;
  /**
    * onError()
    * @description 
    * 监听 WebSocket 错误
    * @param {(result : GeneralCallbackResult) => void} callback
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/socket-task.html#sockettask-onerror
DCloud-yyl's avatar
DCloud-yyl 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548
    * @example
     ```typescript
      task.onError((res) => {
      })
     ```
    */
  onError(callback: (result: GeneralCallbackResult) => void): void;
  /**
    * onMessage()
    * @description 
    * 监听 WebSocket 接受到服务器的消息事件
    * @param {(result : OnSocketMessageCallbackResult) => void} callback
    * @return {void}
    * @tutorial https://uniapp.dcloud.net.cn/api/request/socket-task.html#sockettask-onmessage
DCloud-yyl's avatar
DCloud-yyl 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    * @uniPlatform
	* {
	* 	"app": {
	* 		"android": {
	* 			"osVer": "4.4",
	* 			"uniVer": "√",
	* 			"unixVer": "3.9+"
	* 		},
	* 		"ios": {
	* 			"osVer": "9.0",
	* 			"uniVer": "√",
	* 			"unixVer": "x"
	* 		}
	* 	},
  *   "web": {
  *      "uniVer": "√",
  *      "unixVer": "4.0"
  *   }
	* }
DCloud-yyl's avatar
DCloud-yyl 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    * @example
     ```typescript
      task.onMessage((res) => {
      })
     ```
    */
  onMessage(callback: (result: OnSocketMessageCallbackResult) => void): void;
};
export type OnSocketOpenCallback = (result: OnSocketOpenCallbackResult) => void;

export type OnSocketOpen = (options: OnSocketOpenCallback) => void;
export type OnSocketErrorCallbackResult = {
  /**
   * 错误信息
   */
  errMsg: string
};
export type OnSocketErrorCallback = (result: OnSocketErrorCallbackResult) => void;

export type OnSocketError = (callback: OnSocketErrorCallback) => void;

export type SendSocketMessage = (options: SendSocketMessageOptions) => void;
export type OnSocketMessageCallback = (result: OnSocketMessageCallbackResult) => void;

export type OnSocketMessage = (callback: OnSocketMessageCallback) => void;

export type CloseSocket = (options: CloseSocketOptions) => void;
export type OnSocketCloseCallbackResult = {
  /**
   * 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。	
   */
  code: number,
  /**
   * 一个可读的字符串,表示连接被关闭的原因。	
   */
  reason: string
};
export type OnSocketCloseCallback = (result: OnSocketCloseCallbackResult) => void;

export type OnSocketClose = (callback: OnSocketCloseCallback) => void;