Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
陈庄旺
uni-app
提交
0be526e2
U
uni-app
项目概览
陈庄旺
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0be526e2
编写于
8月 23, 2019
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 解决某些情况下 onSocketOpen 无法监听的问题(h5)
上级
31d00592
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
79 addition
and
83 deletion
+79
-83
src/platforms/h5/service/api/network/socket.js
src/platforms/h5/service/api/network/socket.js
+79
-83
未找到文件。
src/platforms/h5/service/api/network/socket.js
浏览文件 @
0be526e2
var
socketTask
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
const
eventNames
=
[
'
open
'
,
'
close
'
,
'
error
'
,
'
message
'
]
const
callbacks
=
{}
const
socketTasks
=
[]
/**
* SocketTask
*/
...
...
@@ -12,15 +17,55 @@ class SocketTask {
* @param {string} url
* @param {Array} protocols
*/
constructor
(
url
,
protocols
)
{
this
.
_webSocket
=
new
WebSocket
(
url
,
protocols
)
constructor
(
url
,
protocols
,
callback
)
{
let
error
try
{
const
webSocket
=
this
.
_webSocket
=
new
WebSocket
(
url
,
protocols
)
webSocket
.
binaryType
=
'
arraybuffer
'
this
.
_callbacks
=
{}
eventNames
.
forEach
(
name
=>
{
this
.
_callbacks
[
name
]
=
[]
webSocket
.
addEventListener
(
name
,
event
=>
{
const
res
=
name
===
'
message
'
?
{
data
:
event
.
data
}
:
{}
this
.
_callbacks
[
name
].
forEach
(
callback
=>
{
try
{
callback
(
res
)
}
catch
(
e
)
{
console
.
error
(
`thirdScriptError\n
${
e
}
;at socketTask.on
${
name
[
0
].
toUpperCase
()
+
name
.
substr
(
1
)}
callback function\n`
,
e
)
}
})
if
(
this
===
socketTasks
[
0
]
&&
callbacks
[
name
])
{
invoke
(
callbacks
[
name
],
res
)
}
if
(
name
===
'
error
'
||
name
===
'
close
'
)
{
const
index
=
socketTasks
.
indexOf
(
this
)
if
(
index
>=
0
)
{
socketTasks
.
splice
(
index
,
1
)
}
}
})
})
let
propertys
=
[
'
CLOSED
'
,
'
CLOSING
'
,
'
CONNECTING
'
,
'
OPEN
'
,
'
readyState
'
]
propertys
.
forEach
((
property
)
=>
{
Object
.
defineProperty
(
this
,
property
,
{
get
()
{
return
webSocket
[
property
]
}
})
})
}
catch
(
e
)
{
error
=
e
}
callback
(
error
,
this
)
}
/**
* 发送
* @param {any} data
*/
send
(
options
=
{})
{
var
data
=
options
.
data
const
data
=
options
.
data
const
ws
=
this
.
_webSocket
try
{
ws
.
send
(
data
)
...
...
@@ -35,60 +80,19 @@ class SocketTask {
* @param {string} reason
*/
close
(
options
=
{})
{
var
code
=
options
.
data
var
reason
=
options
.
data
const
ws
=
this
.
_webSocket
const
arrgs
=
[]
arrgs
.
push
(
options
.
code
||
1000
)
if
(
typeof
options
.
reason
===
'
string
'
)
{
arrgs
.
push
(
options
.
reason
)
}
try
{
ws
.
close
(
code
,
reason
)
ws
.
close
(
...
arrgs
)
this
.
_callback
(
options
,
'
sendSocketMessage:ok
'
)
}
catch
(
error
)
{
this
.
_callback
(
options
,
`sendSocketMessage:fail
${
error
}
`
)
}
}
/**
* 监听开启
* @param {Function} callback
*/
onOpen
(
callback
)
{
this
.
_on
(
'
open
'
,
callback
)
}
/**
* 监听关闭
* @param {Function} callback
*/
onClose
(
callback
)
{
this
.
_on
(
'
close
'
,
callback
)
}
/**
* 监听错误
* @param {Function} callback
*/
onError
(
callback
)
{
this
.
_on
(
'
error
'
,
callback
)
}
/**
* 监听消息
* @param {Function} callback
*/
onMessage
(
callback
)
{
this
.
_on
(
'
message
'
,
callback
)
}
/**
* 监听事件
* @param {string} eventName
* @param {Function} callback
*/
_on
(
eventName
,
callback
)
{
this
.
_webSocket
.
addEventListener
(
eventName
,
event
=>
{
if
(
eventName
===
'
message
'
)
{
callback
({
data
:
event
.
data
})
}
else
{
callback
()
}
},
false
)
}
/**
* 通用回调处理
*/
...
...
@@ -97,7 +101,7 @@ class SocketTask {
fail
,
complete
},
errMsg
)
{
var
data
=
{
const
data
=
{
errMsg
}
if
(
/:ok$/
.
test
(
errMsg
))
{
...
...
@@ -114,7 +118,12 @@ class SocketTask {
}
}
}
eventNames
.
forEach
(
item
=>
{
const
name
=
item
[
0
].
toUpperCase
()
+
item
.
substr
(
1
)
SocketTask
.
prototype
[
`on
${
name
}
`
]
=
function
(
callback
)
{
this
.
_callbacks
[
item
].
push
(
callback
)
}
})
/**
* 创建一个 WebSocket 连接
* @param {any} data 数据
...
...
@@ -124,16 +133,14 @@ export function connectSocket ({
url
,
protocols
},
callbackId
)
{
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
socketTask
=
new
SocketTask
(
url
,
protocols
)
setTimeout
(()
=>
{
return
new
SocketTask
(
url
,
protocols
,
(
error
,
socketTask
)
=>
{
if
(
!
error
)
{
socketTasks
.
push
(
socketTask
)
}
invoke
(
callbackId
,
{
errMsg
:
'
connectSocket:
ok
'
errMsg
:
'
connectSocket:
'
+
(
error
?
`fail
${
error
}
`
:
'
ok
'
)
})
},
0
)
return
socketTask
})
}
/**
* 通过 WebSocket 连接发送数据
...
...
@@ -141,11 +148,9 @@ export function connectSocket ({
* @param {string} callbackId
*/
export
function
sendSocketMessage
(
options
,
callbackId
)
{
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
if
(
socketTask
&&
socketTask
.
_webSocket
.
readyState
===
WebSocket
.
OPEN
)
{
socketTask
.
send
(
Object
.
assign
(
options
,
{
const
socketTask
=
socketTasks
[
0
]
if
(
socketTask
&&
socketTask
.
readyState
===
socketTask
.
OPEN
)
{
socketTask
.
send
(
Object
.
assign
({},
options
,
{
complete
(
res
)
{
invoke
(
callbackId
,
res
)
}
...
...
@@ -162,11 +167,9 @@ export function sendSocketMessage (options, callbackId) {
* @param {string} callbackId
*/
export
function
closeSocket
(
options
,
callbackId
)
{
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
if
(
socketTask
&&
socketTask
.
_webSocket
.
readyState
!==
WebSocket
.
CLOSED
)
{
socketTask
.
close
(
Object
.
assign
(
options
,
{
const
socketTask
=
socketTasks
[
0
]
if
(
socketTask
)
{
socketTask
.
close
(
Object
.
assign
({},
options
,
{
complete
(
res
)
{
invoke
(
callbackId
,
res
)
}
...
...
@@ -182,34 +185,27 @@ export function closeSocket (options, callbackId) {
* @param {string} method
*/
function
on
(
method
)
{
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
return
function
(
callbackId
)
{
if
(
socketTask
)
{
socketTask
[
method
](
function
(
res
)
{
invoke
(
callbackId
,
res
)
})
}
callbacks
[
method
]
=
callbackId
}
}
/**
* 监听WebSocket连接打开事件
* @param {Function} cb
*/
export
const
onSocketOpen
=
on
(
'
o
nO
pen
'
)
export
const
onSocketOpen
=
on
(
'
open
'
)
/**
* 监听WebSocket错误
* @param {Function} cb
*/
export
const
onSocketError
=
on
(
'
onE
rror
'
)
export
const
onSocketError
=
on
(
'
e
rror
'
)
/**
* 监听WebSocket接受到服务器的消息事件
* @param {Function} cb
*/
export
const
onSocketMessage
=
on
(
'
onM
essage
'
)
export
const
onSocketMessage
=
on
(
'
m
essage
'
)
/**
* 监听WebSocket关闭
* @param {Function} callback
*/
export
const
onSocketClose
=
on
(
'
onC
lose
'
)
export
const
onSocketClose
=
on
(
'
c
lose
'
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录