Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
887c1863
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6373
Star
108
Fork
184
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
24
列表
看板
标记
里程碑
合并请求
2
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
24
Issue
24
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
You need to sign in or sign up before continuing.
提交
887c1863
编写于
4月 21, 2025
作者:
zhaofengliang920817
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新sse示例
上级
64452776
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
91 addition
and
69 deletion
+91
-69
pages.json
pages.json
+1
-1
pages/API/connect-event-source/connect-event-source.uvue
pages/API/connect-event-source/connect-event-source.uvue
+90
-68
未找到文件。
pages.json
浏览文件 @
887c1863
...
...
@@ -1101,7 +1101,7 @@
}
},
//
#endif
//
#ifdef
APP-ANDROID
//
#ifdef
APP-ANDROID
||
APP-IOS
{
"path"
:
"pages/API/connect-event-source/connect-event-source"
,
"group"
:
"1,5,5"
,
...
...
pages/API/connect-event-source/connect-event-source.uvue
浏览文件 @
887c1863
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1">
<!-- #endif -->
<view>
<page-head :title="title"></page-head>
<button class="button" type="primary" @click="connect">连接</button>
<button class="button" type="primary" @click="close">关闭</button>
<text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script>
export default {
data() {
return {
log: '',
title: 'sse',
url: 'https://request.dcloud.net.cn/api/sse/connect',
eventSource: null as UniEventSource | null,
open:false,
receiveMessage:false
}
},
unmounted() {
if(this.eventSource!=null){
this.eventSource?.close()
}
},
methods: {
connect() {
console.log('connect')
this.eventSource?.close()
let headers:UTSJSONObject = new UTSJSONObject()
headers.set("header1","value1")
headers.set("header2","value3")
this.eventSource = uni.connectEventSource({
url: this.url,
header:headers
} )
this.eventSource?.onMessage((ev) => {
this.log +='type:' + ev.type + '\ndata:' + ev.data + '\n\n'
this.receiveMessage = true
})
this.eventSource?.onOpen((ev) => {
this.log += 'open:' + ev.type+ '\n\n'
this.open = true
})
this.eventSource?.onError((ev) => {
this.log += 'error:' + ev.errMsg+ '\n\n'
})
console.log('connect end')
},
close() {
this.eventSource?.close()
}
}
}
</script>
<style>
.button {
margin-left: 30px;
margin-right: 30px;
margin-bottom: 15px;
}
<page-head :title="title"></page-head>
<button class="button" type="primary" @click="connect">连接</button>
<button class="button" type="primary" @click="close">关闭</button>
<!-- #ifdef APP -->
<scroll-view style="flex:1">
<!-- #endif -->
<view>
<text style="width: 100%; text-align: center; margin-bottom: 5px;">
显示简易操作日志(可滚动查看)
</text>
<button size="mini" @click="logList = []">清空日志</button>
<view style="margin-top: 10px;">
<view v-for="(item, index) in logList" :key="index">
<text style="margin-left: 20px; margin-right: 20px;">
{{ item }}
</text>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script>
export default {
data() {
return {
logList: [] as string[],
title: 'sse',
url: 'https://request.dcloud.net.cn/api/sse/connect',
eventSource: null as UniEventSource | null,
open: false,
receiveMessage: false
}
},
unmounted() {
if (this.eventSource != null) {
this.eventSource?.close()
}
},
methods: {
connect() {
console.log('connect start')
uni.showLoading({
title: "",
mask: true
})
this.eventSource?.close()
let headers : UTSJSONObject = new UTSJSONObject()
headers.set("header1", "value1")
headers.set("header2", "value3")
this.eventSource = uni.connectEventSource({
url: this.url,
header: headers
})
this.eventSource?.onMessage((ev) => {
const log = 'onMessage callback:' + '\n' + 'type: ' + ev.type + '\n' + 'data: ' + ev.data + '\n\n'
this.logList.push(log)
this.receiveMessage = true
uni.hideLoading()
})
this.eventSource?.onOpen((ev) => {
const log = 'onOpen callback: ' + ev.type + '\n\n'
this.logList.push(log)
this.open = true
})
this.eventSource?.onError((err) => {
const log = `onError callback: ${err} \n\n`
this.logList.push(log)
uni.hideLoading()
})
},
close() {
this.eventSource?.close()
const log = 'connect close' + '\n\n'
this.logList.push(log)
}
}
}
</script>
<style>
.button {
margin-left: 30px;
margin-right: 30px;
margin-bottom: 15px;
}
</style>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录