package com.yksbogazici.owl.framework.data.chat.remote import android.util.Log import com.microsoft.signalr.HubConnection import javax.inject.Inject class ChatSocketDataSource @Inject constructor( private val connection: HubConnection ) { fun startConnection() { connection.start().blockingAwait() Log.i("signalr", "connection state: ${connection.connectionState}") } fun sendMessage(userId: String, message: String) { connection.invoke("SendMessage", userId, message) Log.i("signalr", "sendMessage: $message") } fun receiveMessage(onMessageReceived: (ChatSocketResponse) -> Unit) { connection.on( "ReceiveMessage", { senderId, receiverId, message, timestamp -> Log.i("signalr", "receiveMessage: $senderId $receiverId $message $timestamp") onMessageReceived( ChatSocketResponse( senderId, receiverId, message, timestamp ) ) }, String::class.java, String::class.java, String::class.java, String::class.java ) } fun closeConnection() { connection.stop() } }