socket.ts 1.4 KB
Newer Older
1
import { isString } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5
import { elemInArray, HTTP_METHODS } from '../../helpers/protocol'
export const API_CONNECT_SOCKET = 'connectSocket'
export type API_TYPE_CONNECT_SOCKET = typeof uni.connectSocket
export const ConnectSocketOptions: ApiOptions<API_TYPE_CONNECT_SOCKET> = {
fxy060608's avatar
fxy060608 已提交
6
  formatArgs: {
fxy060608's avatar
fxy060608 已提交
7
    header(value: Record<string, any>, params: Record<string, any>) {
fxy060608's avatar
fxy060608 已提交
8 9 10
      params.header = value || {}
    },
    method(value, params) {
fxy060608's avatar
fxy060608 已提交
11 12 13 14
      params.method = elemInArray(
        (value || '').toUpperCase(),
        HTTP_METHODS
      ) as any
fxy060608's avatar
fxy060608 已提交
15 16
    },
    protocols(protocols, params) {
17
      if (isString(protocols)) {
fxy060608's avatar
fxy060608 已提交
18 19
        params.protocols = [protocols]
      }
20 21
    },
  },
fxy060608's avatar
fxy060608 已提交
22 23
}

fxy060608's avatar
fxy060608 已提交
24
export const ConnectSocketProtocol: ApiProtocol<API_TYPE_CONNECT_SOCKET> = {
fxy060608's avatar
fxy060608 已提交
25 26
  url: {
    type: String,
27
    required: true,
fxy060608's avatar
fxy060608 已提交
28 29
  },
  header: {
30
    type: Object,
fxy060608's avatar
fxy060608 已提交
31
  },
fxy060608's avatar
fxy060608 已提交
32 33
  method: String as any,
  protocols: [Array, String] as any,
fxy060608's avatar
fxy060608 已提交
34
}
fxy060608's avatar
fxy060608 已提交
35 36 37 38

export const API_SEND_SOCKET_MESSAGE = 'sendSocketMessage'
export type API_TYPE_SEND_SOCKET_MESSAGE = typeof uni.sendSocketMessage

39 40 41 42
export const SendSocketMessageProtocol: ApiProtocol<API_TYPE_SEND_SOCKET_MESSAGE> =
  {
    data: [String, ArrayBuffer],
  }
fxy060608's avatar
fxy060608 已提交
43 44 45

export const API_CLOSE_SOCKET = 'closeSocket'
export type API_TYPE_CLOSE_SOCKET = typeof uni.closeSocket
Q
qiang 已提交
46
export const CloseSocketProtocol: ApiProtocol<API_TYPE_CLOSE_SOCKET> = {
fxy060608's avatar
fxy060608 已提交
47 48
  code: Number,
  reason: String,
fxy060608's avatar
fxy060608 已提交
49
}