davidliu
Committed by GitHub

Handle data received from server (#187)

... ... @@ -140,8 +140,11 @@ sealed class RoomEvent(val room: Room) : Event() {
/**
* Received data published by another participant
*
* @param data the published data
* @param participant the participant if available
*/
class DataReceived(room: Room, val data: ByteArray, val participant: RemoteParticipant) : RoomEvent(room)
class DataReceived(room: Room, val data: ByteArray, val participant: RemoteParticipant?) : RoomEvent(room)
/**
* The connection quality for a participant has changed.
... ...
... ... @@ -699,12 +699,12 @@ constructor(
* @suppress
*/
override fun onUserPacket(packet: LivekitModels.UserPacket, kind: LivekitModels.DataPacket.Kind) {
val participant = remoteParticipants[packet.participantSid] ?: return
val participant = remoteParticipants[packet.participantSid]
val data = packet.payload.toByteArray()
listener?.onDataReceived(data, participant, this)
eventBus.postEvent(RoomEvent.DataReceived(this, data, participant), coroutineScope)
participant.onDataReceived(data)
participant?.onDataReceived(data)
}
/**
... ... @@ -1027,7 +1027,7 @@ interface RoomListener {
/**
* Received data published by another participant
*/
fun onDataReceived(data: ByteArray, participant: RemoteParticipant, room: Room) {}
fun onDataReceived(data: ByteArray, participant: RemoteParticipant?, room: Room) {}
/**
* The connection quality for a participant has changed.
... ...
... ... @@ -108,7 +108,7 @@ class CallViewModel(
when (it) {
is RoomEvent.FailedToConnect -> mutableError.value = it.error
is RoomEvent.DataReceived -> {
val identity = it.participant.identity ?: ""
val identity = it.participant?.identity ?: "server"
val message = it.data.toString(Charsets.UTF_8)
mutableDataReceived.emit("$identity: $message")
}
... ...