提交 1aaf8d7e 编写于 作者: D DCloud_LXH

fix(App): socket、package

上级 af196316
......@@ -19,7 +19,17 @@ import { extend, capitalize } from '@vue/shared'
import { callOptions } from '@dcloudio/uni-shared'
type Socket = {
send: ({ data }: { data: string | ArrayBuffer }) => void
send: ({
data,
}: {
id: String
data:
| string
| {
'@type': String
base64: String
}
}) => void
close: ({ code, reason }: { code?: number; reason?: string }) => void
onopen: Function
onmessage: Function
......@@ -102,38 +112,13 @@ class SocketTask implements UniApp.SocketTask {
})
})
this._socket.onerror(() => {
this.onErrorOrClose()
this.socketStateChange('error')
this.onErrorOrClose()
})
this._socket.onclose(() => {
this.onErrorOrClose()
this.socketStateChange('close')
this.onErrorOrClose()
})
const oldSocketSend = this._socket.send
const oldSocketClose = this._socket.close
this._socket.send = (res) => {
oldSocketSend(
extend({
id: this.id,
data:
typeof res.data === 'object'
? {
'@type': 'binary',
base64: arrayBufferToBase64(res.data),
}
: res.data,
})
)
}
this._socket.close = (res) => {
oldSocketClose(
extend({
id: this.id,
res,
})
)
}
}
onErrorOrClose() {
......@@ -163,7 +148,14 @@ class SocketTask implements UniApp.SocketTask {
}
try {
this._socket.send({
data: args.data,
id: this.id,
data:
typeof args.data === 'object'
? {
'@type': 'binary',
base64: arrayBufferToBase64(args.data),
}
: args.data,
})
callOptions(args, 'sendSocketMessage:ok')
} catch (error) {
......@@ -174,7 +166,12 @@ class SocketTask implements UniApp.SocketTask {
close(args: UniApp.CloseSocketOptions) {
this.readyState = this.CLOSING
try {
this._socket.close(args)
this._socket.close(
extend({
id: this.id,
args,
})
)
callOptions(args, 'closeSocket:ok')
} catch (error) {
callOptions(args, `closeSocket:fail ${error}`)
......@@ -232,9 +229,7 @@ export const sendSocketMessage = defineAsyncApi<API_TYPE_SEND_SOCKET_MESSAGE>(
reject('sendSocketMessage:fail WebSocket is not connected')
return
}
socketTask._socket.send({
data: args.data,
})
socketTask.send({ data: args.data })
resolve()
},
SendSocketMessageProtocol
......@@ -250,7 +245,7 @@ export const closeSocket = defineAsyncApi<API_TYPE_CLOSE_SOCKET>(
}
socketTask.readyState = socketTask.CLOSING
const { code, reason } = args
socketTask._socket.close({ code, reason })
socketTask.close({ code, reason })
resolve()
},
CloseSocketProtocol
......
......@@ -816,9 +816,10 @@
version "0.2.3"
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
"@dcloudio/types@^2.3.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.3.0.tgz#e983fe73a6b5b7b8f72df7ddbfe7ab4a2853596a"
"@dcloudio/types@^2.3.1":
version "2.3.1"
resolved "https://registry.nlark.com/@dcloudio/types/download/@dcloudio/types-2.3.1.tgz?cache=0&sync_timestamp=1625822205457&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40dcloudio%2Ftypes%2Fdownload%2F%40dcloudio%2Ftypes-2.3.1.tgz#3e09724368f8d9d26f92052f56f555df3d2dc313"
integrity sha1-PglyQ2j42dJvkgUvVvVV3z0twxM=
"@eslint/eslintrc@^0.4.2":
version "0.4.2"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册