Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the posibility to send headers for sockets connections #142

Open
wants to merge 1 commit into
base: 0.2.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import com.tinder.scarlet.socketio.SocketIoEvent
import com.tinder.scarlet.utils.SimpleChannelFactory
import com.tinder.scarlet.utils.SimpleProtocolOpenRequestFactory
import io.socket.client.IO
import io.socket.client.Manager
import io.socket.client.Socket
import io.socket.engineio.client.Transport
import org.json.JSONObject

class SocketIoClient(
private val url: () -> String,
private val options: IO.Options = IO.Options()
private val url: () -> String,
private val requestHeaders: () -> RequestHeaders = { RequestHeaders(mapOf()) },
private val options: IO.Options = IO.Options()
) : Protocol {

override fun createChannelFactory(): Channel.Factory {
Expand All @@ -32,15 +35,18 @@ class SocketIoClient(

override fun createOpenRequestFactory(channel: Channel): Protocol.OpenRequest.Factory {
return SimpleProtocolOpenRequestFactory {
MainChannelOpenRequest(url())
MainChannelOpenRequest(url(), requestHeaders())
}
}

override fun createEventAdapterFactory(): ProtocolSpecificEventAdapter.Factory {
return SocketIoEvent.Adapter.Factory()
}

data class MainChannelOpenRequest(val url: String) : Protocol.OpenRequest
data class MainChannelOpenRequest(val url: String,
val requestHeaders: RequestHeaders) : Protocol.OpenRequest

data class RequestHeaders(val headers: Map<String, String>)
}

class SocketIoEventName(
Expand Down Expand Up @@ -71,7 +77,10 @@ internal class SocketIoMainChannel(

override fun open(openRequest: Protocol.OpenRequest) {
val mainChannelOpenRequest = openRequest as SocketIoClient.MainChannelOpenRequest
val socket = IO.socket(mainChannelOpenRequest.url, options)
val socket = IO.socket(mainChannelOpenRequest.url, options).apply {
addRequestHeaders(mainChannelOpenRequest.requestHeaders)
}

socket
.on(Socket.EVENT_CONNECT) {
listener.onOpened(this)
Expand Down Expand Up @@ -168,4 +177,19 @@ internal class SocketIoMessageChannel(
}
return true
}
}

fun Socket.addRequestHeaders(requestHeaders: SocketIoClient.RequestHeaders) {
io().on(Manager.EVENT_TRANSPORT) { transportData ->

val transport = transportData[0] as Transport

transport.on(Transport.EVENT_REQUEST_HEADERS) { headersData ->
val headersOut = headersData[0] as MutableMap<String, List<String>>

requestHeaders.headers.forEach { (key, value) ->
headersOut[key] = listOf(value)
}
}
}
}