1
1
package com.coder.gateway.sdk
2
2
3
3
import com.coder.gateway.models.BrokerMessage
4
- import com.coder.gateway.models.UriScheme
5
4
import com.coder.gateway.models.Workspace
6
5
import com.google.gson.Gson
7
6
import com.google.gson.GsonBuilder
@@ -30,6 +29,7 @@ import okhttp3.WebSocket
30
29
import okhttp3.WebSocketListener
31
30
import okhttp3.logging.HttpLoggingInterceptor
32
31
import okio.ByteString
32
+ import okio.ByteString.Companion.encodeUtf8
33
33
import java.net.URL
34
34
import java.time.Instant
35
35
import java.util.logging.Logger
@@ -112,18 +112,19 @@ class Tunneler(val brokerAddr: URL, val token: String, val workspace: Workspace,
112
112
logger.info(" created peer connection" )
113
113
114
114
logger.info(" creating control channel { \" proto\" : \" control\" }" )
115
- rtc.createDataChannel(" control" , RTCDataChannelInit ().apply {
115
+ rtc.createDataChannel(" control" , RTCDataChannelInit ().apply {
116
116
protocol = " control"
117
117
ordered = true
118
118
})
119
- rtc.createOffer(RTCOfferOptions (), object : CreateSessionDescriptionObserver {
119
+ rtc.createOffer(RTCOfferOptions (), object : CreateSessionDescriptionObserver {
120
120
override fun onSuccess (sessionDescription : RTCSessionDescription ? ) {
121
121
logger.info(" created offer {\" offer\" : ${sessionDescription} }" )
122
122
rtc.setLocalDescription(sessionDescription, object : SetSessionDescriptionObserver {
123
123
override fun onSuccess () {
124
124
logger.info(" set local offer $sessionDescription with success" )
125
125
val offerMsg = BrokerMessage (sessionDescription!! , options.iceServers, options.turnRemoteProxyURL.toString())
126
126
logger.info(" sending offer message {\" msg\" : ${gson.toJson(offerMsg)} }" )
127
+ connection.send(gson.toJson(offerMsg).encodeUtf8())
127
128
}
128
129
129
130
override fun onFailure (p0 : String? ) {
@@ -134,18 +135,20 @@ class Tunneler(val brokerAddr: URL, val token: String, val workspace: Workspace,
134
135
}
135
136
136
137
override fun onFailure (p0 : String? ) {
138
+ logger.warning(" onFailure to set local $p0 with success" )
137
139
TODO (" Not yet implemented" )
138
140
}
139
141
})
140
142
141
- return Dialer (connection,rtc.createDataChannel(" data_channel_tmp" , RTCDataChannelInit ()), rtc)
143
+ return Dialer (connection, rtc.createDataChannel(" data_channel_tmp" , RTCDataChannelInit ()), rtc)
142
144
}
145
+
143
146
private fun newPeerConnection (servers : List <RTCIceServer >, dialer : TURNProxyDialer , connection : WebSocket ): RTCPeerConnection {
144
147
val configuration = RTCConfiguration ().apply {
145
148
iceServers = servers
146
149
if (servers.size == 1 ) {
147
150
val url = iceServers[0 ].urls[0 ]
148
- if (url.startsWith( " turn" ) || url.startsWith(" turns" )) {
151
+ if (url.startsWith(" turn" ) || url.startsWith(" turns" )) {
149
152
this .iceTransportPolicy = RTCIceTransportPolicy .RELAY
150
153
}
151
154
}
@@ -182,9 +185,7 @@ data class TURNProxyDialer(val baseURL: URL, val token: String)
182
185
* inside a workspace. The opposing end of the WebSocket messages
183
186
* should be proxied with a Listener.
184
187
*/
185
- class Dialer (val connection : WebSocket , val ctrl : RTCDataChannel , val rtc : RTCPeerConnection ) {
186
-
187
- }
188
+ class Dialer (val connection : WebSocket , val ctrl : RTCDataChannel , val rtc : RTCPeerConnection )
188
189
189
190
data class ICECandidateInit (
190
191
@SerializedName(" candidate" ) val candidate : String ,
0 commit comments