Skip to content
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
21 changes: 14 additions & 7 deletions src/main/kotlin/org/phoenixframework/Socket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class Socket(
* that contain the ref of the message to send and the callback that will send the message.
*/
internal var sendBuffer: MutableList<Pair<String?, () -> Unit>> = ArrayList()
private val sendBufferLock = Any()

/** Ref counter for messages */
internal var ref: Int = 0
Expand Down Expand Up @@ -415,7 +416,9 @@ class Socket(
} else {
// If the socket is not connected, add the push to a buffer which will
// be sent immediately upon connection.
sendBuffer.add(Pair(ref, callback))
synchronized(sendBufferLock) {
sendBuffer.add(Pair(ref, callback))
}
}
}

Expand Down Expand Up @@ -464,17 +467,21 @@ class Socket(

/** Send all messages that were buffered before the socket opened */
internal fun flushSendBuffer() {
if (isConnected && sendBuffer.isNotEmpty()) {
this.sendBuffer.forEach { it.second.invoke() }
this.sendBuffer.clear()
synchronized(sendBufferLock) {
if (isConnected && sendBuffer.isNotEmpty()) {
this.sendBuffer.forEach { it.second.invoke() }
this.sendBuffer.clear()
}
}
}

/** Removes an item from the send buffer with the matching ref */
internal fun removeFromSendBuffer(ref: String) {
this.sendBuffer = this.sendBuffer
.filter { it.first != ref }
.toMutableList()
synchronized(sendBufferLock) {
this.sendBuffer = this.sendBuffer
.filter { it.first != ref }
.toMutableList()
}
}

internal fun leaveOpenTopic(topic: String) {
Expand Down