socket.ts 1.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
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 已提交
5 6 7 8 9
  formatArgs: {
    header(value, params) {
      params.header = value || {}
    },
    method(value, params) {
fxy060608's avatar
fxy060608 已提交
10 11 12 13
      params.method = elemInArray(
        (value || '').toUpperCase(),
        HTTP_METHODS
      ) as any
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18
    },
    protocols(protocols, params) {
      if (typeof protocols === 'string') {
        params.protocols = [protocols]
      }
19 20
    },
  },
fxy060608's avatar
fxy060608 已提交
21 22
}

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

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

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

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