Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libraries/apollo-runtime/api/android/apollo-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public final class com/apollographql/apollo/network/ws/AppSyncWsProtocol$Factory

public final class com/apollographql/apollo/network/ws/DefaultWebSocketEngine : com/apollographql/apollo/network/ws/WebSocketEngine {
public fun <init> ()V
public fun <init> (Lkotlin/jvm/functions/Function0;)V
public fun <init> (Lokhttp3/WebSocket$Factory;)V
public fun open (Ljava/lang/String;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
Expand Down
1 change: 1 addition & 0 deletions libraries/apollo-runtime/api/jvm/apollo-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public final class com/apollographql/apollo/network/ws/AppSyncWsProtocol$Factory

public final class com/apollographql/apollo/network/ws/DefaultWebSocketEngine : com/apollographql/apollo/network/ws/WebSocketEngine {
public fun <init> ()V
public fun <init> (Lkotlin/jvm/functions/Function0;)V
public fun <init> (Lokhttp3/WebSocket$Factory;)V
public fun open (Ljava/lang/String;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import java.util.concurrent.atomic.AtomicReference
import okhttp3.WebSocket as PlatformWebSocket
import okhttp3.WebSocketListener as PlatformWebSocketListener

internal class JvmWebSocketEngine(private val webSocketFactory: PlatformWebSocket.Factory) : WebSocketEngine {
internal class JvmWebSocketEngine(webSocketFactory: () -> PlatformWebSocket.Factory) : WebSocketEngine {
private val webSocketFactory by lazy { webSocketFactory() }

constructor(webSocketFactory: PlatformWebSocket.Factory) : this({ webSocketFactory })

var closed = false
override fun newWebSocket(url: String, headers: List<HttpHeader>, listener: WebSocketListener): WebSocket {
require(!closed) {
Expand Down Expand Up @@ -88,11 +92,11 @@ internal class JvmWebSocket(
}

private fun List<HttpHeader>.toOkHttpHeaders(): Headers =
Headers.Builder().also { headers ->
this.forEach {
headers.add(it.name, it.value)
}
}.build()
Headers.Builder().also { headers ->
this.forEach {
headers.add(it.name, it.value)
}
}.build()

override fun send(data: ByteArray) {
platformWebSocket.get()?.send(data.toByteString())
Expand All @@ -110,7 +114,21 @@ internal class JvmWebSocket(
}


actual fun WebSocketEngine(): WebSocketEngine = JvmWebSocketEngine(defaultOkHttpClientBuilder.build())
actual fun WebSocketEngine(): WebSocketEngine = JvmWebSocketEngine { defaultOkHttpClientBuilder.build() }

/**
* Creates a new [WebSocketEngine] from [webSocketFactory]
*
* This factory function accepts a function so that OkHttp is initialized from a background thread
*/
@ApolloExperimental
fun WebSocketEngine(webSocketFactory: () -> PlatformWebSocket.Factory): WebSocketEngine = JvmWebSocketEngine(webSocketFactory)

/**
* Creates a new [WebSocketEngine] from [webSocketFactory]
*
* Prefer using the factory function accepting a function so that OkHttp is initialized from a background thread.
* See https://github.com/square/okhttp/pull/8248
*/
@ApolloExperimental
fun WebSocketEngine(webSocketFactory: PlatformWebSocket.Factory): WebSocketEngine = JvmWebSocketEngine(webSocketFactory)
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ import okhttp3.WebSocket
import okhttp3.WebSocketListener
import okio.ByteString

/**
* Creates a new [DefaultWebSocketEngine] from [webSocketFactory]
*
* This constructor accepts a function so that OkHttp is initialized from a background thread
*/
actual class DefaultWebSocketEngine(
private val webSocketFactory: WebSocket.Factory,
webSocketFactory: () -> WebSocket.Factory,
) : WebSocketEngine {

private val webSocketFactory by lazy { webSocketFactory() }

/**
* Creates a new [DefaultWebSocketEngine] from [webSocketFactory]
*
* Prefer using the factory function accepting a function so that OkHttp is initialized from a background thread.
* See https://github.com/square/okhttp/pull/8248
*/
constructor(webSocketFactory: WebSocket.Factory): this({webSocketFactory})
actual constructor() : this(
webSocketFactory = defaultOkHttpClientBuilder.build()
)
Expand Down
Loading