# uni.connectEventSource(options)

连接 SSE

SSE,全称是Server-sent Events,一种服务器基于http向客户端推送文本消息的技术。

提供本API(uni.connectEventSource)的主要目的是兼容Web的SSE规范。但如果开发者需要接收AI大语言模型的数据,实际上无法使用SSE。

因为SSE仅支持get,无法post数据。LLM是用户post一个prompt,然后流式获取结果。

所以LLM流式接收数据的场景,应该使用uni.request的Chunk,而不是使用本API(uni.connectEventSource)。详见

# connectEventSource 兼容性

Web 微信小程序 Android iOS iOS uni-app x UTS 插件 HarmonyOS
x - 4.51 4.63 4.63 x

Web端暂未兼容uni.connectEventSource API,请使用标准的Web API。

小程序不支持SSE,替代方案也是使用uni.request的Chunk。

# 参数

名称 类型 必填 默认值 兼容性 描述
options ConnectEventSourceOptions -
名称 类型 必备 默认值 兼容性 描述
url string -
服务器地址
header UTSJSONObject -
请求头

# 返回值

类型
UniEventSource

# UniEventSource 的方法

# onMessage(callback : ConnectEventSourceCallback) : void

onMessage message 事件,会在通过事件源收到数据时触发。

# onMessage 兼容性
Web 微信小程序 Android iOS iOS uni-app x UTS 插件 HarmonyOS
x - 4.51 4.63 4.63 x
# 参数
名称 类型 必填 默认值 兼容性 描述
callback (ev: UniMessageEvent) => void -
事件回调
# UniMessageEvent 的属性值
名称 类型 必备 默认值 兼容性 描述
type string null
事件类型。
data any null
消息发射器发出的数据。
lastEventId string null
一个字符串,表示事件的唯一 ID。

# onError(callback : ConnectEventSourceErrorCallback) : void

onError onerror 是当发生错误且这个错误事件(error)被 UniEventSource 触发时调用的一个事件处理函数。

# onError 兼容性
Web 微信小程序 Android iOS iOS uni-app x UTS 插件 HarmonyOS
x - 4.51 4.63 4.63 x
# 参数
名称 类型 必填 默认值 兼容性 描述
callback (error: UniError) => void -
事件回调

# onOpen(callback : ConnectEventSourceCallback) : void

onOpen 一个事件处理器,它在收到 open 事件时被调用,在那时,连接刚被打开。

# onOpen 兼容性
Web 微信小程序 Android iOS iOS uni-app x UTS 插件 HarmonyOS
x - 4.51 4.63 4.63 x
# 参数
名称 类型 必填 默认值 兼容性 描述
callback (ev: UniMessageEvent) => void -
事件回调

# close() : void

close 关闭当前的连接

# close 兼容性
Web 微信小程序 Android iOS iOS uni-app x UTS 插件 HarmonyOS
x - 4.51 4.63 4.63 x

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验

扫码体验(手机浏览器跳转到App直达页)
<template>

  <page-head :title="data.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="data.logList = []">清空日志</button>
			<view style="margin-top: 10px;">
				<view v-for="(item, index) in data.logList" :key="index">
					<text style="margin-left: 20px; margin-right: 20px;">
						{{ item }}
					</text>
				</view>
			</view>
		</view>
	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
</template>

<script setup lang="uts">
	type DataType = {
		logList: string[];
		title: string;
		url: string;
		eventSource: UniEventSource | null;
		open: boolean;
		receiveMessage: boolean;
	}

	const data = reactive({
		logList: [] as string[],
		title: 'sse',
		url: 'https://request.dcloud.net.cn/api/sse/connect',
		eventSource: null,
		open: false,
		receiveMessage: false
	} as DataType)

	onUnmounted(() => {
		if (data.eventSource != null) {
			data.eventSource?.close()
		}
	})

	const connect = () => {
		console.log('connect start')
		uni.showLoading({
			title: "",
			mask: true
		})
		data.eventSource?.close()
		let headers : UTSJSONObject = new UTSJSONObject()
		headers.set("header1", "value1")
		headers.set("header2", "value3")
		data.eventSource = uni.connectEventSource({
			url: data.url,
			header: headers
		})
		data.eventSource?.onMessage((ev) => {
			const log = 'onMessage callback:' + '\n' + 'type: ' + ev.type + '\n' + 'data: ' + ev.data + '\n\n'
			data.logList.push(log)
			data.receiveMessage = true
			uni.hideLoading()
		})
		data.eventSource?.onOpen((ev) => {
			const log = 'onOpen callback: ' + ev.type + '\n\n'
			data.logList.push(log)
			data.open = true
		})
		data.eventSource?.onError((err) => {
			const log = `onError callback: ${err} \n\n`
			data.logList.push(log)
			uni.hideLoading()
		})
	}

	const close = () => {
		data.eventSource?.close()
		const log = 'connect close' + '\n\n'
		data.logList.push(log)
	}

	defineExpose({
    data,
		connect
	})
</script>

<style>
	.button {
		margin-left: 30px;
		margin-right: 30px;
		margin-bottom: 15px;
	}
</style>

# 参见

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验

扫码体验(手机浏览器跳转到App直达页)
<template>

  <page-head :title="data.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="data.logList = []">清空日志</button>
			<view style="margin-top: 10px;">
				<view v-for="(item, index) in data.logList" :key="index">
					<text style="margin-left: 20px; margin-right: 20px;">
						{{ item }}
					</text>
				</view>
			</view>
		</view>
	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
</template>

<script setup lang="uts">
	type DataType = {
		logList: string[];
		title: string;
		url: string;
		eventSource: UniEventSource | null;
		open: boolean;
		receiveMessage: boolean;
	}

	const data = reactive({
		logList: [] as string[],
		title: 'sse',
		url: 'https://request.dcloud.net.cn/api/sse/connect',
		eventSource: null,
		open: false,
		receiveMessage: false
	} as DataType)

	onUnmounted(() => {
		if (data.eventSource != null) {
			data.eventSource?.close()
		}
	})

	const connect = () => {
		console.log('connect start')
		uni.showLoading({
			title: "",
			mask: true
		})
		data.eventSource?.close()
		let headers : UTSJSONObject = new UTSJSONObject()
		headers.set("header1", "value1")
		headers.set("header2", "value3")
		data.eventSource = uni.connectEventSource({
			url: data.url,
			header: headers
		})
		data.eventSource?.onMessage((ev) => {
			const log = 'onMessage callback:' + '\n' + 'type: ' + ev.type + '\n' + 'data: ' + ev.data + '\n\n'
			data.logList.push(log)
			data.receiveMessage = true
			uni.hideLoading()
		})
		data.eventSource?.onOpen((ev) => {
			const log = 'onOpen callback: ' + ev.type + '\n\n'
			data.logList.push(log)
			data.open = true
		})
		data.eventSource?.onError((err) => {
			const log = `onError callback: ${err} \n\n`
			data.logList.push(log)
			uni.hideLoading()
		})
	}

	const close = () => {
		data.eventSource?.close()
		const log = 'connect close' + '\n\n'
		data.logList.push(log)
	}

	defineExpose({
    data,
		connect
	})
</script>

<style>
	.button {
		margin-left: 30px;
		margin-right: 30px;
		margin-bottom: 15px;
	}
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息