@@ -6,6 +6,7 @@ import WebRTC.RTCDataBuffer
6
6
import WebRTC.RTCDataChannel
7
7
import WebRTC.RTCDataChannelDelegateProtocol
8
8
import WebRTC.RTCDataChannelState
9
+ import kotlinx.cinterop.BetaInteropApi
9
10
import kotlinx.cinterop.ExperimentalForeignApi
10
11
import kotlinx.coroutines.MainScope
11
12
import kotlinx.coroutines.cancel
@@ -35,22 +36,10 @@ actual class DataChannel(val ios: RTCDataChannel) {
35
36
private val coroutineScope = MainScope ()
36
37
private val dataChannelEvent = MutableSharedFlow <DataChannelEvent >()
37
38
38
- init {
39
- ios.delegate = object : NSObject (), RTCDataChannelDelegateProtocol {
40
- override fun dataChannel (dataChannel : RTCDataChannel , didChangeBufferedAmount : uint64_t) {
41
- // not implemented
42
- }
43
-
44
- override fun dataChannel (dataChannel : RTCDataChannel , didReceiveMessageWithBuffer : RTCDataBuffer ) {
45
- coroutineScope.launch {
46
- dataChannelEvent.emit(DataChannelEvent .MessageReceived (didReceiveMessageWithBuffer))
47
- }
48
- }
39
+ private val delegate = Delegate ()
49
40
50
- override fun dataChannelDidChangeState (dataChannel : RTCDataChannel ) {
51
- coroutineScope.launch { dataChannelEvent.emit(DataChannelEvent .StateChanged ) }
52
- }
53
- }
41
+ init {
42
+ ios.delegate = delegate
54
43
}
55
44
56
45
actual val onOpen: Flow <Unit > = dataChannelEvent
@@ -72,6 +61,7 @@ actual class DataChannel(val ios: RTCDataChannel) {
72
61
.filterNotNull()
73
62
.map { it.buffer.data.toByteArray() }
74
63
64
+ @BetaInteropApi
75
65
actual fun send (data : ByteArray ): Boolean {
76
66
val buffer = RTCDataBuffer (data.toNSData(), true )
77
67
return ios.sendData(buffer)
@@ -99,4 +89,20 @@ actual class DataChannel(val ios: RTCDataChannel) {
99
89
else -> error(" Unknown RTCDataChannelState: $state " )
100
90
}
101
91
}
92
+
93
+ private inner class Delegate : NSObject (), RTCDataChannelDelegateProtocol {
94
+ override fun dataChannel (dataChannel : RTCDataChannel , didChangeBufferedAmount : uint64_t) {
95
+ // not implemented
96
+ }
97
+
98
+ override fun dataChannel (dataChannel : RTCDataChannel , didReceiveMessageWithBuffer : RTCDataBuffer ) {
99
+ coroutineScope.launch {
100
+ dataChannelEvent.emit(DataChannelEvent .MessageReceived (didReceiveMessageWithBuffer))
101
+ }
102
+ }
103
+
104
+ override fun dataChannelDidChangeState (dataChannel : RTCDataChannel ) {
105
+ coroutineScope.launch { dataChannelEvent.emit(DataChannelEvent .StateChanged ) }
106
+ }
107
+ }
102
108
}
0 commit comments