Skip to content

Commit ec36b7b

Browse files
authored
Merge pull request #85 from dsrees/feature/encode-decode-closures
Added EncodeClosure and DecodeClosure
2 parents 11f8ed0 + d5009f2 commit ec36b7b

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

README.md

+27-8
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,8 @@ socket.connect() // or internal reconnect logic kicks in
5858
```
5959

6060

61-
You can also inject your own OkHttp Client and GSON into the Socket to provide your own configuration
61+
You can also inject your own OkHttp Client into the Socket to provide your own configuration
6262
```kotlin
63-
64-
// Configure your own GSON instance
65-
val gson = Gson.Builder().create()
66-
6763
// Configure your own OkHttp Client
6864
val client = OkHttpClient.Builder()
6965
.connectTimeout(1000, TimeUnit.MILLISECONDS)
@@ -72,14 +68,37 @@ val client = OkHttpClient.Builder()
7268
// Create Socket with your custom instances
7369
val params = hashMapOf("token" to "abc123")
7470
val socket = Socket("http://localhost:4000/socket/websocket",
75-
params,
76-
gson,
77-
client)
71+
params,
72+
client)
73+
```
74+
75+
By default, the client use GSON to encode and decode JSON. If you prefer to manage this yourself, you
76+
can provide custom encode/decode functions in the `Socket` constructor.
77+
78+
```kotlin
79+
80+
// Configure your own GSON instance
81+
val gson = Gson.Builder().create()
82+
val encoder: EncodeClosure = {
83+
// Encode a Map into JSON using your custom GSON instance or another JSON library
84+
// of your choice (Moshi, etc)
85+
}
86+
val decoder: DecodeClosure = {
87+
// Decode a JSON String into a `Message` object using your custom JSON library
88+
}
89+
90+
// Create Socket with your custom instances
91+
val params = hashMapOf("token" to "abc123")
92+
val socket = Socket("http://localhost:4000/socket/websocket",
93+
params,
94+
encoder,
95+
decoder)
7896
```
7997

8098

8199

82100

101+
83102
### Installation
84103

85104
JavaPhoenixClient is hosted on MavenCentral. You'll need to make sure you declare `mavenCentral()` as one of your repositories

src/main/kotlin/org/phoenixframework/Defaults.kt

+16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ object Defaults {
5555
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
5656
.create()
5757

58+
/**
59+
* Default JSON decoder, backed by GSON, that takes JSON and converts it
60+
* into a Message object.
61+
*/
62+
val decode: DecodeClosure = { rawMessage ->
63+
gson.fromJson(rawMessage, Message::class.java)
64+
}
65+
66+
/**
67+
* Default JSON encoder, backed by GSON, that takes a Map<String, Any> and
68+
* converts it into a JSON String.
69+
*/
70+
val encode: EncodeClosure = { payload ->
71+
gson.toJson(payload)
72+
}
73+
5874
/**
5975
* Takes an endpoint and a params closure given by the User and constructs a URL that
6076
* is ready to be sent to the Socket connection.

src/main/kotlin/org/phoenixframework/Socket.kt

+18-7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ const val WS_CLOSE_ABNORMAL = 1006
104104
*/
105105
typealias PayloadClosure = () -> Payload?
106106

107+
/** A closure that will encode a Map<String, Any> into a JSON String */
108+
typealias EncodeClosure = (Map<String, Any>) -> String
109+
110+
/** A closure that will decode a JSON String into a [Message] */
111+
typealias DecodeClosure = (String) -> Message
112+
113+
107114
/**
108115
* Connects to a Phoenix Server
109116
*/
@@ -118,13 +125,15 @@ typealias PayloadClosure = () -> Payload?
118125
* ```
119126
* @param url Url to connect to such as https://example.com/socket
120127
* @param paramsClosure Closure which allows to change parameters sent during connection.
121-
* @param gson Default GSON Client to parse JSON. You can provide your own if needed.
128+
* @param encode Optional. Provide a custom JSON encoding implementation
129+
* @param decode Optional. Provide a custom JSON decoding implementation
122130
* @param client Default OkHttpClient to connect with. You can provide your own if needed.
123131
*/
124132
class Socket(
125133
url: String,
126134
val paramsClosure: PayloadClosure,
127-
private val gson: Gson = Defaults.gson,
135+
private val encode: EncodeClosure = Defaults.encode,
136+
private val decode: DecodeClosure = Defaults.decode,
128137
private val client: OkHttpClient = OkHttpClient.Builder().build()
129138
) {
130139

@@ -226,15 +235,17 @@ class Socket(
226235
*
227236
* @param url Url to connect to such as https://example.com/socket
228237
* @param params Constant parameters to send when connecting. Defaults to null
229-
* @param gson Default GSON Client to parse JSON. You can provide your own if needed.
238+
* @param encode Optional. Provide a custom JSON encoding implementation
239+
* @param decode Optional. Provide a custom JSON decoding implementation
230240
* @param client Default OkHttpClient to connect with. You can provide your own if needed.
231241
*/
232242
constructor(
233243
url: String,
234244
params: Payload? = null,
235-
gson: Gson = Defaults.gson,
245+
encode: EncodeClosure = Defaults.encode,
246+
decode: DecodeClosure = Defaults.decode,
236247
client: OkHttpClient = OkHttpClient.Builder().build()
237-
) : this(url, { params }, gson, client)
248+
) : this(url, { params }, encode, decode, client)
238249

239250
init {
240251
var mutableUrl = url
@@ -387,7 +398,7 @@ class Socket(
387398
ref?.let { body["ref"] = it }
388399
joinRef?.let { body["join_ref"] = it }
389400

390-
val data = gson.toJson(body)
401+
val data = this.encode(body)
391402
connection?.let { transport ->
392403
this.logItems("Push: Sending $data")
393404
transport.send(data)
@@ -569,7 +580,7 @@ class Socket(
569580
this.logItems("Receive: $rawMessage")
570581

571582
// Parse the message as JSON
572-
val message = gson.fromJson(rawMessage, Message::class.java)
583+
val message = this.decode(rawMessage)
573584

574585
// Clear heartbeat ref, preventing a heartbeat timeout disconnect
575586
if (message.ref == pendingHeartbeatRef) pendingHeartbeatRef = null

0 commit comments

Comments
 (0)