mirror of https://github.com/oxen-io/session-ios
add data channel
parent
0e473e35a7
commit
9030710d9d
@ -0,0 +1,34 @@
|
||||
import WebRTC
|
||||
import Foundation
|
||||
|
||||
extension WebRTCSession: RTCDataChannelDelegate {
|
||||
|
||||
internal func createDataChannel() {
|
||||
let dataChannelConfiguration = RTCDataChannelConfiguration()
|
||||
dataChannelConfiguration.isOrdered = true
|
||||
dataChannelConfiguration.isNegotiated = true
|
||||
dataChannelConfiguration.maxRetransmits = 30
|
||||
dataChannelConfiguration.maxPacketLifeTime = 30000
|
||||
dataChannel = peerConnection.dataChannel(forLabel: "DATACHANNEL", configuration: dataChannelConfiguration)
|
||||
dataChannel?.delegate = self
|
||||
}
|
||||
|
||||
public func sendJSON(_ json: JSON) {
|
||||
if let dataChannel = self.dataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) {
|
||||
let dataBuffer = RTCDataBuffer(data: jsonAsData, isBinary: false)
|
||||
dataChannel.sendData(dataBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Data channel delegate
|
||||
public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) {
|
||||
print("[Calls] Data channed did change to \(dataChannel.readyState)")
|
||||
}
|
||||
|
||||
public func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) {
|
||||
print("[Calls] Data channel did receive data: \(buffer)")
|
||||
if let json = try? JSONSerialization.jsonObject(with: buffer.data, options: [ .fragmentsAllowed ]) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue