Skip to content

Commit

Permalink
Add dedicated function for disconnect callback (#279)
Browse files Browse the repository at this point in the history
* Add dedicated function for disconnect callback

* Update readme.md file
  • Loading branch information
RakshakGaind0910 authored Mar 3, 2023
1 parent 714bd24 commit 17d913c
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ We can then use this method to create a listener that listens for an invitation
// Handle errors - Update UI or Navigate to new screen, etc.
}

override fun onSocketDisconnect() {
// Handle disconnect - Update UI or Navigate to login screen, etc.
}

})
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class CallInstanceFragment : Fragment(R.layout.fragment_call_instance), NumberKe
//NOOP
}

override fun onSocketDisconnect() {
//NOOP
}

})
}

Expand All @@ -160,12 +164,12 @@ class CallInstanceFragment : Fragment(R.layout.fragment_call_instance), NumberKe
}

override fun onLeftAuxButtonClicked() {
//NOOP
//NOOP
}

override fun onNumberClicked(number: Int) {
mainViewModel.dtmfPressed(callId!!, number.toString())
when(number) {
when (number) {
0 -> {
toneGenerator.startTone(TONE_DTMF_0, 500)
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/telnyx/webrtc/sdk/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ class MainActivity : AppCompatActivity() {
message ?: "Socket Connection Error",
Toast.LENGTH_SHORT
).show()
}

override fun onSocketDisconnect() {
Toast.makeText(
this@MainActivity,
"Socket is disconnected",
Toast.LENGTH_SHORT
).show()

progress_indicator_id.visibility = View.INVISIBLE
incoming_call_section_id.visibility = View.GONE
call_control_section_id.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class MainViewModel @Inject constructor(
}

fun disconnect() {
telnyxClient?.disconnect()
telnyxClient?.onDisconnect()
userManager.isUserLogin = false
}

Expand Down
4 changes: 4 additions & 0 deletions telnyx_rtc/src/main/java/com/telnyx/webrtc/sdk/Call.kt
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ class Call(
//NOOP
}

override fun onDisconnect() {
//NOOP
}

override fun onClientReady(jsonObject: JsonObject) {
// NOOP
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,8 @@ class TelnyxClient(
* @see [ConnectivityHelper]
* @see [TxSocket]
*/
fun disconnect(disconnectMessage: String = "Socket disconnected") {
socketResponseLiveData.postValue(SocketResponse.error(disconnectMessage))
override fun onDisconnect() {
socketResponseLiveData.postValue(SocketResponse.disconnect())
invalidateGatewayResponseTimer()
resetGatewayCounters()
unregisterNetworkCallback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ package com.telnyx.webrtc.sdk.model
* @property MESSAGERECEIVED the socket has received a message
* @property ERROR the socket has encountered an error
* @property LOADING the socket is loading a connection
* @property DISCONNECT when the socket is disconnect
*/
enum class SocketStatus {
ESTABLISHED,
MESSAGERECEIVED,
ERROR,
LOADING
LOADING,
DISCONNECT
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TxSocket(
internal var ongoingCall = false
internal var isLoggedIn = false
internal var isConnected = false
internal var isPing=false
internal var isPing = false

private lateinit var client: OkHttpClient
private lateinit var socket: WebSocket
Expand Down Expand Up @@ -171,7 +171,7 @@ class TxSocket(
}

PINGPONG.methodName -> {
isPing =true
isPing = true
webSocket.send(text)
listener.pingPong()
}
Expand Down Expand Up @@ -203,14 +203,14 @@ class TxSocket(
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
super.onClosing(webSocket, code, reason)
Timber.tag("TxSocket").i("Socket is closing: $code :: $reason")
listener.disconnect("Socket is closed: $code :: $reason")
listener.onDisconnect()
}

override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
super.onClosed(webSocket, code, reason)
Timber.tag("TxSocket").i("Socket is closed: $code :: $reason")
destroy()
listener.disconnect("Socket is closed: $code :: $reason")
listener.onDisconnect()
}

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ interface TxSocketListener {
fun setCallRecovering()

fun pingPong()
/**
* Fires when the socket has disconnected
* */
fun onDisconnect()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class SocketObserver<T> : Observer<SocketResponse<T>> {
abstract fun onMessageReceived(data: T?)
abstract fun onLoading()
abstract fun onError(message: String?)
abstract fun onSocketDisconnect()

override fun onChanged(t: SocketResponse<T>?) {
if (t == null) {
Expand All @@ -23,6 +24,7 @@ abstract class SocketObserver<T> : Observer<SocketResponse<T>> {
SocketStatus.MESSAGERECEIVED -> onMessageReceived(t.data)
SocketStatus.LOADING -> onLoading()
SocketStatus.ERROR -> onError(t.errorMessage)
SocketStatus.DISCONNECT -> onSocketDisconnect()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ data class SocketResponse<out T>(val status: SocketStatus, val data: T?, val err
fun <T> error(msg: String): SocketResponse<T> {
return SocketResponse(SocketStatus.ERROR, null, msg)
}
fun <T> disconnect(): SocketResponse<T> {
return SocketResponse(SocketStatus.DISCONNECT, null, null)
}

fun <T> loading(): SocketResponse<T> {
return SocketResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TelnyxClientTest : BaseTest() {
)
)

client.disconnect()
client.onDisconnect()
assertEquals(client.isNetworkCallbackRegistered, false)
assertEquals(client.socket.isConnected, false)
assertEquals(client.socket.isLoggedIn, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class TxSocketTest : BaseTest() {
// Sleep to give time to connect
Thread.sleep(1000)

client.disconnect()
client.onDisconnect()
Thread.sleep(1000)
verify(client.socket, atLeastOnce()).destroy()
}
Expand Down

0 comments on commit 17d913c

Please sign in to comment.