Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
b6b97c02
U
uni-app
项目概览
DCloud
/
uni-app
2 个月 前同步成功
通知
717
Star
38705
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b6b97c02
编写于
6月 24, 2021
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(App): socket
上级
f0be3312
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
272 addition
and
0 deletion
+272
-0
packages/uni-app-plus/src/service/api/network/socket.ts
packages/uni-app-plus/src/service/api/network/socket.ts
+272
-0
未找到文件。
packages/uni-app-plus/src/service/api/network/socket.ts
0 → 100644
浏览文件 @
b6b97c02
import
{
defineTaskApi
,
defineAsyncApi
,
defineOnApi
,
API_CONNECT_SOCKET
,
API_TYPE_CONNECT_SOCKET
,
ConnectSocketProtocol
,
ConnectSocketOptions
,
API_SEND_SOCKET_MESSAGE
,
API_TYPE_SEND_SOCKET_MESSAGE
,
SendSocketMessageProtocol
,
API_CLOSE_SOCKET
,
API_TYPE_CLOSE_SOCKET
,
CloseSocketProtocol
,
}
from
'
@dcloudio/uni-api
'
import
{
requireNativePlugin
}
from
'
../base
'
import
{
base64ToArrayBuffer
,
arrayBufferToBase64
}
from
'
@dcloudio/uni-api
'
import
{
extend
,
capitalize
}
from
'
@vue/shared
'
import
{
callOptions
}
from
'
@dcloudio/uni-shared
'
type
Socket
=
{
send
:
({
data
}:
{
data
:
string
|
ArrayBuffer
})
=>
void
close
:
({
code
,
reason
}:
{
code
?:
number
;
reason
?:
string
})
=>
void
onopen
:
Function
onmessage
:
Function
onerror
:
Function
onclose
:
Function
WebSocket
:
Function
}
type
eventName
=
keyof
WebSocketEventMap
const
socketTasks
:
SocketTask
[]
=
[]
const
globalEvent
:
Record
<
eventName
,
string
>
=
{
open
:
''
,
close
:
''
,
error
:
''
,
message
:
''
,
}
let
socket
:
Socket
export
function
createSocketTask
(
args
:
UniApp
.
ConnectSocketOption
)
{
const
socketId
=
String
(
Date
.
now
())
let
errMsg
try
{
if
(
!
socket
)
{
socket
=
requireNativePlugin
(
'
uni-webSocket
'
)
}
socket
.
WebSocket
({
id
:
socketId
,
url
:
args
.
url
,
protocol
:
Array
.
isArray
(
args
.
protocols
)
?
args
.
protocols
.
join
(
'
,
'
)
:
args
.
protocols
,
header
:
args
.
header
,
})
}
catch
(
error
)
{
errMsg
=
error
}
return
{
socket
,
socketId
,
errMsg
}
}
class
SocketTask
implements
UniApp
.
SocketTask
{
private
_callbacks
:
{
open
:
Function
[]
close
:
Function
[]
error
:
Function
[]
message
:
Function
[]
}
_socket
:
Socket
id
:
string
CLOSED
:
number
CLOSING
:
number
CONNECTING
:
number
OPEN
:
number
readyState
:
number
constructor
(
socket
:
Socket
,
socketId
:
string
)
{
this
.
id
=
socketId
this
.
_socket
=
socket
this
.
_callbacks
=
{
open
:
[],
close
:
[],
error
:
[],
message
:
[],
}
this
.
CLOSED
=
3
this
.
CLOSING
=
2
this
.
CONNECTING
=
0
this
.
OPEN
=
1
this
.
readyState
=
this
.
CLOSED
if
(
!
this
.
_socket
)
return
this
.
_socket
.
onopen
(()
=>
{
this
.
readyState
=
this
.
OPEN
this
.
socketStateChange
(
'
open
'
)
})
this
.
_socket
.
onmessage
((
e
:
any
)
=>
{
this
.
socketStateChange
(
'
message
'
,
{
data
:
typeof
e
.
data
===
'
object
'
?
base64ToArrayBuffer
(
e
.
data
.
base64
)
:
e
.
data
,
})
})
this
.
_socket
.
onerror
(()
=>
{
this
.
onErrorOrClose
()
this
.
socketStateChange
(
'
error
'
)
})
this
.
_socket
.
onclose
(()
=>
{
this
.
onErrorOrClose
()
this
.
socketStateChange
(
'
close
'
)
})
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
()
{
this
.
readyState
=
this
.
CLOSED
const
index
=
socketTasks
.
indexOf
(
this
)
if
(
index
>=
0
)
{
socketTasks
.
splice
(
index
,
1
)
}
}
socketStateChange
(
name
:
eventName
,
res
:
Data
=
{})
{
if
(
this
===
socketTasks
[
0
]
&&
globalEvent
[
name
])
{
UniServiceJSBridge
.
invokeOnCallback
(
globalEvent
[
name
],
res
)
}
// WYQ fix: App平台修复websocket onOpen时发送数据报错的Bug
this
.
_callbacks
[
name
].
forEach
((
callback
)
=>
{
if
(
typeof
callback
===
'
function
'
)
{
callback
(
name
===
'
message
'
?
res
:
{})
}
})
}
send
(
args
:
UniApp
.
SendSocketMessageOptions
)
{
if
(
this
.
readyState
!==
this
.
OPEN
)
{
callOptions
(
args
,
'
sendSocketMessage:fail WebSocket is not connected
'
)
}
try
{
this
.
_socket
.
send
({
data
:
args
.
data
,
})
callOptions
(
args
,
'
sendSocketMessage:ok
'
)
}
catch
(
error
)
{
callOptions
(
args
,
`sendSocketMessage:fail
${
error
}
`
)
}
}
close
(
args
:
UniApp
.
CloseSocketOptions
)
{
this
.
readyState
=
this
.
CLOSING
try
{
this
.
_socket
.
close
(
args
)
callOptions
(
args
,
'
closeSocket:ok
'
)
}
catch
(
error
)
{
callOptions
(
args
,
`closeSocket:fail
${
error
}
`
)
}
}
onOpen
(
callback
:
Function
)
{
this
.
_callbacks
.
open
.
push
(
callback
)
}
onClose
(
callback
:
Function
)
{
this
.
_callbacks
.
close
.
push
(
callback
)
}
onError
(
callback
:
Function
)
{
this
.
_callbacks
.
error
.
push
(
callback
)
}
onMessage
(
callback
:
Function
)
{
this
.
_callbacks
.
message
.
push
(
callback
)
}
}
export
const
connectSocket
=
defineTaskApi
<
API_TYPE_CONNECT_SOCKET
>
(
API_CONNECT_SOCKET
,
({
url
,
protocols
,
header
,
method
},
{
resolve
,
reject
})
=>
{
const
{
socket
,
socketId
,
errMsg
}
=
createSocketTask
({
url
,
protocols
,
header
,
method
,
})
const
socketTask
=
new
SocketTask
(
socket
,
socketId
)
if
(
errMsg
)
{
setTimeout
(()
=>
{
reject
(
errMsg
)
},
0
)
}
else
{
socketTasks
.
push
(
socketTask
)
}
setTimeout
(()
=>
{
resolve
()
},
0
)
return
socketTask
},
ConnectSocketProtocol
,
ConnectSocketOptions
)
export
const
sendSocketMessage
=
defineAsyncApi
<
API_TYPE_SEND_SOCKET_MESSAGE
>
(
API_SEND_SOCKET_MESSAGE
,
(
args
,
{
resolve
,
reject
})
=>
{
const
socketTask
=
socketTasks
[
0
]
if
(
!
socketTask
||
socketTask
.
readyState
!==
socketTask
.
OPEN
)
{
reject
(
'
sendSocketMessage:fail WebSocket is not connected
'
)
return
}
socketTask
.
_socket
.
send
({
data
:
args
.
data
,
})
resolve
()
},
SendSocketMessageProtocol
)
export
const
closeSocket
=
defineAsyncApi
<
API_TYPE_CLOSE_SOCKET
>
(
API_CLOSE_SOCKET
,
(
args
,
{
resolve
,
reject
})
=>
{
const
socketTask
=
socketTasks
[
0
]
if
(
!
socketTask
)
{
reject
(
'
closeSocket:fail WebSocket is not connected
'
)
return
}
socketTask
.
readyState
=
socketTask
.
CLOSING
const
{
code
,
reason
}
=
args
socketTask
.
_socket
.
close
({
code
,
reason
})
resolve
()
},
CloseSocketProtocol
)
function
on
(
event
:
eventName
)
{
const
api
=
`onSocket
${
capitalize
(
event
)}
`
return
defineOnApi
(
api
,
()
=>
{
globalEvent
[
event
]
=
api
})
}
export
const
onSocketOpen
=
/*#__PURE__*/
on
(
'
open
'
)
export
const
onSocketError
=
/*#__PURE__*/
on
(
'
error
'
)
export
const
onSocketMessage
=
/*#__PURE__*/
on
(
'
message
'
)
export
const
onSocketClose
=
/*#__PURE__*/
on
(
'
close
'
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录