-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Ahmad Saleh on 24/06/2024. | ||
// | ||
|
||
import Foundation | ||
import WebRTC | ||
|
||
public class EdgeDataChannelImpl: NSObject, EdgeDataChannel, RTCDataChannelDelegate { | ||
internal var dc: RTCDataChannel! | ||
public var onMessage: EdgeOnMessageCallback? = nil | ||
public var onOpened: EdgeOnOpenedCallback? = nil | ||
public var onClosed: EdgeOnClosedCallback? = nil | ||
|
||
public init(_ dataChannel: RTCDataChannel) { | ||
self.dc = dataChannel | ||
super.init() | ||
|
||
self.dc.delegate = self | ||
} | ||
|
||
public func send(_ data: Data) async { | ||
let buffer = RTCDataBuffer(data: data, isBinary: true) | ||
self.dc.sendData(buffer) | ||
} | ||
|
||
public func close() async { | ||
self.dc.close() | ||
} | ||
|
||
public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { | ||
EdgeLogger.info("Data channel \(dataChannel.channelId) state changed to \(dataChannel.readyState)") | ||
switch dataChannel.readyState { | ||
case .connecting: | ||
break | ||
|
||
case .open: | ||
self.onOpened?() | ||
break | ||
|
||
case .closing: | ||
break | ||
|
||
case .closed: | ||
self.onClosed?() | ||
break | ||
|
||
@unknown default: | ||
break | ||
} | ||
} | ||
|
||
public func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) { | ||
self.onMessage?(buffer.data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters