use anyhow::Result;
use bytes::Bytes;
use std::sync::Arc;
use webrtc::data_channel::data_channel_message::DataChannelMessage;
use webrtc::data_channel::RTCDataChannel;
pub struct DataChannelWrapper {
channel: Arc<RTCDataChannel>,
}
impl DataChannelWrapper {
pub fn new(channel: Arc<RTCDataChannel>) -> Self {
Self { channel }
}
pub async fn send(&self, text: &str) -> Result<()> {
self.channel.send(&Bytes::from(text.to_string())).await?;
Ok(())
}
pub async fn send_binary(&self, data: &[u8]) -> Result<()> {
self.channel.send(&Bytes::copy_from_slice(data)).await?;
Ok(())
}
pub fn on_message<F>(&self, callback: F)
where
F: Fn(DataChannelMessage) + Send + Sync + 'static,
{
let cb = Arc::new(callback);
self.channel.on_message(Box::new(move |msg| {
let cb = Arc::clone(&cb);
Box::pin(async move {
cb(msg);
})
}));
}
pub fn label(&self) -> String {
self.channel.label().to_string()
}
}