Hi everyone , im again facing a issue in Ktor Clin...
# ktor
h
Hi everyone , im again facing a issue in Ktor Clint side , im able to send message from my device but not able to receving I dont know what happening , im explaing a code that i prove . here we have
initSession()
used to connect to the websocket and
messageObservable()
to observe incomming frames
Copy code
class ChatSocketServiceImp(private val httpClient: HttpClient) : ChatSocketService {

    private var  socket : WebSocketSession? = null

    override suspend fun initSession(userId: String): Resource<Unit> {
        val url = URLBuilder().apply {
            takeFrom(ChatSocketService.BASE_URL)
            path("chat-socket")
            parameters.append("userId" , userId)
        }.build()

       return  try {
             socket = httpClient.webSocketSession {
                url(url)
            }
            if (socket?.isActive == true)
            {
                Log.d("TAG", "socket Connected")
                Resource.Success(Unit)
            }else
            {
                Resource.Error("Couldn't estabish Connection connection")
            }
        }catch (e : Exception){
            e.printStackTrace()
            Resource.Error(e.localizedMessage ?: "Unknown Error")
        }
    }

  

    override fun messageObservable(): Flow<MessageReceive> {
        Log.d("socket", "inside messageObservable ")
       return try {
           Log.d("socket", "inside messageObservable inin ")
           socket?.incoming
               ?.receiveAsFlow()
               ?.filter { it is Frame.Text }
               ?.map {
                   Log.d("socket", "inside messageObservable inin2 ${it.data} ")
                   val json = (it as? Frame.Text)?.readText() ?: ""
                   val message = Json.decodeFromString<MessageReceive>(json)
                   Log.d("socket", "message -> ${message.message}")
                   message
               } ?: flow {  }
       }catch (e : Exception){
           Log.d("socket", "Exception -> ${e.message}")
           e.printStackTrace()
           flow {  }
       }
    }
}
Im accessing these mehods in my ViewModel
Copy code
fun init(chatId : String)
{
    viewModelScope.launch {
      val result =   chatSocketService.initSession(chatId)
        when(result){
           is Resource.Success ->{
               chatSocketService.messageObservable().onEach { message ->
                   Log.d("TAG", "init:  ${message.message} ${message.senderId} ${message.recipientId} ${message.timeStamp} ")
                   _messages.value = _messages.value.copy(
                        id = message.id,
                       message = message.message,
                       senderId = message.senderId,
                       recipientId = message.recipientId,
                       timeStamp = message.timeStamp
                   )
               }
            }
            is Resource.Error ->{
                Log.d("TAG", "init: ERROR IN CONNECTIONG TO SOCKET")
            }
        }
    }
}
*Not receiving Frames , im my logges socket is not null it connectd to the server and
messageObservable()
not triggering try catch
stackoverflow 1
image.png
a
Can you try receiving the frames with a simplest code possible and without the dependencies to isolate the issue?
h
It's working now it's a deserialization issue