微信小程序不支持
EventSource(SSE)是一种HTML5标准,旨在提供一种方式来在Web应用中实现异步通信和事件处理。
然而,根据现有的信息,uni-app并没有直接支持EventSource,这意味着开发者需要寻找其他技术或替代方案来实现类似的效果。
尽管有些主流浏览器支持SSE,但小程序目前还不能兼容这个API,这可能是由于微信平台对新技术的兼容性考虑或是开发生态的限制。
一个替代实现方式
class EventSource {
constructor(url, retryTime = 0) {
this.url = url;
this.retryTime = retryTime;
this.listeners = {};
this.requestTask = null
this.connect()
}
connect() {
this.requestTask = wx.request({
url: this.url,
enableChunked: true,
responseType: 'text',
method: 'GET',
timeout: 300e3,
success: res => {
this.emit('success', res)
if (this.retryTime > 0) {
setTimeout(() => {
this.connect()
}, this.retryTime)
}
},
fail: () => {
}
});