1
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"minSdkVersion": "21",
|
||||
"dependencies":[
|
||||
"com.squareup.okhttp3:okhttp-sse:3.12.2",
|
||||
"com.squareup.okhttp3:okhttp:3.12.2"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import SSEClient from 'uts.sdk.modules.xSseUtsModule.SSEClient';
|
||||
import {xSSEOptions} from "../interface.uts"
|
||||
export class SSEClientApp {
|
||||
private sseClient : SSEClient
|
||||
url : string = ''
|
||||
private onOpenEvt = () => { }
|
||||
private onErrorEvt = () => { }
|
||||
private onClosedEvt = () => { }
|
||||
private onMessageEvt = (data : string) => { }
|
||||
|
||||
constructor(options:xSSEOptions) {
|
||||
this.url = options.url;
|
||||
let _headers = new Map<string, string>()
|
||||
const headersOpts = options?.header??({} as UTSJSONObject)
|
||||
for(const key in headersOpts){
|
||||
_headers.set(key,headersOpts.getString(key)!)
|
||||
}
|
||||
this.sseClient = SSEClient(this.url, _headers)
|
||||
|
||||
}
|
||||
@UTSJS.keepAlive
|
||||
onOpen(callback : () => void) {
|
||||
this.onOpenEvt = callback
|
||||
}
|
||||
@UTSJS.keepAlive
|
||||
onError(callback : () => void) {
|
||||
this.onErrorEvt = callback
|
||||
}
|
||||
@UTSJS.keepAlive
|
||||
onClosed(callback : () => void) {
|
||||
this.onClosedEvt = callback
|
||||
}
|
||||
@UTSJS.keepAlive
|
||||
onMessage(callback : (data : string) => void) {
|
||||
this.onMessageEvt = callback
|
||||
}
|
||||
connect() {
|
||||
if(this.isConnected()) return;
|
||||
this.sseClient.connect()
|
||||
let _this = this;
|
||||
class sslins implements SSEClient.SSEListener {
|
||||
override onOpen() {
|
||||
// console.log("SSE", "连接已打开")
|
||||
_this.onOpenEvt()
|
||||
}
|
||||
override onMessage(event : string | null, data : string) {
|
||||
_this.onMessageEvt(data)
|
||||
}
|
||||
override onError(throwable : Throwable) {
|
||||
console.log("SSE", "发生错误", throwable)
|
||||
_this.onErrorEvt()
|
||||
}
|
||||
override onClosed() {
|
||||
// console.log("SSE", "连接已关闭")
|
||||
_this.onClosedEvt()
|
||||
}
|
||||
}
|
||||
this.sseClient.setListener(new sslins())
|
||||
}
|
||||
disconnect() {
|
||||
this.sseClient.disconnect()
|
||||
}
|
||||
isConnected() : boolean {
|
||||
return this.sseClient.isConnected();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package uts.sdk.modules.xSseUtsModule
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import okhttp3.sse.EventSource
|
||||
import okhttp3.sse.EventSourceListener
|
||||
import okhttp3.sse.EventSources
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class SSEClient(private val url: String, private val headers: Map<String, String> = mapOf()) {
|
||||
private var eventSource: EventSource? = null
|
||||
private var isConnected = false
|
||||
private var listener: SSEListener? = null
|
||||
|
||||
// 连接状态回调接口
|
||||
interface SSEListener {
|
||||
fun onOpen()
|
||||
fun onMessage(event: String?, data: String)
|
||||
fun onError(throwable: Throwable)
|
||||
fun onClosed()
|
||||
}
|
||||
|
||||
private val client = OkHttpClient.Builder()
|
||||
.readTimeout(0, TimeUnit.SECONDS) // SSE 需要禁用读取超时
|
||||
.retryOnConnectionFailure(true)
|
||||
.build()
|
||||
|
||||
fun setListener(listener: SSEListener) {
|
||||
this.listener = listener
|
||||
}
|
||||
|
||||
fun connect() {
|
||||
if (isConnected) return
|
||||
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.header("Accept", "text/event-stream")
|
||||
.header("Cache-Control", "no-cache")
|
||||
|
||||
// 添加自定义头信息
|
||||
headers.forEach { (key, value) ->
|
||||
request.header(key, value)
|
||||
}
|
||||
|
||||
val builtRequest = request.build()
|
||||
|
||||
eventSource = EventSources.createFactory(client)
|
||||
.newEventSource(builtRequest, object : EventSourceListener() {
|
||||
override fun onOpen(eventSource: EventSource, response: Response) {
|
||||
isConnected = true
|
||||
listener?.onOpen()
|
||||
}
|
||||
|
||||
override fun onEvent(
|
||||
eventSource: EventSource,
|
||||
id: String?,
|
||||
type: String?,
|
||||
data: String
|
||||
) {
|
||||
listener?.onMessage(type, data)
|
||||
}
|
||||
|
||||
override fun onClosed(eventSource: EventSource) {
|
||||
isConnected = false
|
||||
listener?.onClosed()
|
||||
}
|
||||
|
||||
override fun onFailure(
|
||||
eventSource: EventSource,
|
||||
t: Throwable?,
|
||||
response: Response?
|
||||
) {
|
||||
isConnected = false
|
||||
t?.let { listener?.onError(it) }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
eventSource?.cancel()
|
||||
eventSource = null
|
||||
isConnected = false
|
||||
}
|
||||
|
||||
fun isConnected(): Boolean = isConnected
|
||||
}
|
||||
Reference in New Issue
Block a user