com.pexip.sdk.api.infinity.internal.RealEventSource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-api Show documentation
Show all versions of sdk-api Show documentation
A set of common classes and interfaces to build APIs.
The newest version!
/*
* Copyright 2022-2024 Pexip AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pexip.sdk.api.infinity.internal
import com.pexip.sdk.api.EventSource
import com.pexip.sdk.api.EventSourceListener
import com.pexip.sdk.api.infinity.InfinityEvent
import com.pexip.sdk.api.infinity.UnknownEvent
import okhttp3.Response
internal class RealEventSource(
factory: okhttp3.sse.EventSource.Factory,
request: okhttp3.Request,
listener: EventSourceListener,
) : EventSource {
private val l = object : okhttp3.sse.EventSourceListener() {
override fun onOpen(eventSource: okhttp3.sse.EventSource, response: Response) {
listener.onOpen(this@RealEventSource)
}
override fun onEvent(
eventSource: okhttp3.sse.EventSource,
id: String?,
type: String?,
data: String,
) {
val event = InfinityEvent(id, type, data)
if (event is UnknownEvent) return
listener.onEvent(this@RealEventSource, event)
}
override fun onFailure(
eventSource: okhttp3.sse.EventSource,
t: Throwable?,
response: Response?,
) {
listener.onClosed(this@RealEventSource, t)
}
override fun onClosed(eventSource: okhttp3.sse.EventSource) {
listener.onClosed(this@RealEventSource, null)
}
}
private val source = factory.newEventSource(request, l)
override fun cancel() = source.cancel()
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy