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

fix(App): socket、package

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