2222
2323package org.phoenixframework
2424
25- import com.google.gson.Gson
26- import okhttp3.HttpUrl
2725import okhttp3.OkHttpClient
2826import okhttp3.Response
2927import java.net.URL
@@ -105,7 +103,7 @@ const val WS_CLOSE_ABNORMAL = 1006
105103typealias PayloadClosure = () -> Payload ?
106104
107105/* * A closure that will encode a Map<String, Any> into a JSON String */
108- typealias EncodeClosure = (Map < String , Any > ) -> String
106+ typealias EncodeClosure = (Any ) -> String
109107
110108/* * A closure that will decode a JSON String into a [Message] */
111109typealias DecodeClosure = (String ) -> Message
@@ -125,13 +123,15 @@ typealias DecodeClosure = (String) -> Message
125123 * ```
126124 * @param url Url to connect to such as https://example.com/socket
127125 * @param paramsClosure Closure which allows to change parameters sent during connection.
126+ * @param vsn JSON Serializer version to use. Defaults to 2.0.0
128127 * @param encode Optional. Provide a custom JSON encoding implementation
129128 * @param decode Optional. Provide a custom JSON decoding implementation
130129 * @param client Default OkHttpClient to connect with. You can provide your own if needed.
131130 */
132131class Socket (
133132 url : String ,
134133 val paramsClosure : PayloadClosure ,
134+ val vsn : String = Defaults .VSN ,
135135 private val encode : EncodeClosure = Defaults .encode,
136136 private val decode : DecodeClosure = Defaults .decode,
137137 private val client : OkHttpClient = OkHttpClient .Builder ().build()
@@ -235,17 +235,19 @@ class Socket(
235235 *
236236 * @param url Url to connect to such as https://example.com/socket
237237 * @param params Constant parameters to send when connecting. Defaults to null
238+ * @param vsn JSON Serializer version to use. Defaults to 2.0.0
238239 * @param encode Optional. Provide a custom JSON encoding implementation
239240 * @param decode Optional. Provide a custom JSON decoding implementation
240241 * @param client Default OkHttpClient to connect with. You can provide your own if needed.
241242 */
242243 constructor (
243244 url: String ,
244245 params: Payload ? = null ,
246+ vsn: String = Defaults .VSN ,
245247 encode: EncodeClosure = Defaults .encode,
246248 decode: DecodeClosure = Defaults .decode,
247249 client: OkHttpClient = OkHttpClient .Builder ().build()
248- ) : this (url, { params }, encode, decode, client)
250+ ) : this (url, { params }, vsn, encode, decode, client)
249251
250252 init {
251253 var mutableUrl = url
@@ -266,7 +268,7 @@ class Socket(
266268
267269 // Store the URL that will be used to establish a connection. Could potentially be
268270 // different at the time connect() is called based on a changing params closure.
269- this .endpointUrl = Defaults .buildEndpointUrl(this .endpoint, this .paramsClosure)
271+ this .endpointUrl = Defaults .buildEndpointUrl(this .endpoint, this .paramsClosure, this .vsn )
270272
271273 // Create reconnect timer
272274 this .reconnectTimer = TimeoutTimer (
@@ -305,7 +307,7 @@ class Socket(
305307
306308 // Build the new endpointUrl with the params closure. The payload returned
307309 // from the closure could be different such as a changing authToken.
308- this .endpointUrl = Defaults .buildEndpointUrl(this .endpoint, this .paramsClosure)
310+ this .endpointUrl = Defaults .buildEndpointUrl(this .endpoint, this .paramsClosure, this .vsn )
309311
310312 // Now create the connection transport and attempt to connect
311313 this .connection = this .transport(endpointUrl)
@@ -390,14 +392,7 @@ class Socket(
390392 ) {
391393
392394 val callback: (() -> Unit ) = {
393- val body = mutableMapOf<String , Any >()
394- body[" topic" ] = topic
395- body[" event" ] = event
396- body[" payload" ] = payload
397-
398- ref?.let { body[" ref" ] = it }
399- joinRef?.let { body[" join_ref" ] = it }
400-
395+ val body = listOf (joinRef, ref, topic, event, payload)
401396 val data = this .encode(body)
402397 connection?.let { transport ->
403398 this .logItems(" Push: Sending $data " )
0 commit comments