add wasm ws support

This commit is contained in:
Toshit Chawda 2024-01-28 11:20:41 -08:00
parent 8f85828e73
commit e95d148488
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
7 changed files with 277 additions and 25 deletions

View file

@ -28,10 +28,11 @@ impl From<Frame<'_>> for crate::ws::Frame {
}
}
impl From<crate::ws::Frame> for Frame<'_> {
fn from(frame: crate::ws::Frame) -> Self {
impl TryFrom<crate::ws::Frame> for Frame<'_> {
type Error = crate::WispError;
fn try_from(frame: crate::ws::Frame) -> Result<Self, Self::Error> {
use crate::ws::OpCode::*;
match frame.opcode {
Ok(match frame.opcode {
Text => Self::text(Payload::Owned(frame.payload.to_vec())),
Binary => Self::binary(Payload::Owned(frame.payload.to_vec())),
Close => Self::close_raw(Payload::Owned(frame.payload.to_vec())),
@ -42,7 +43,7 @@ impl From<crate::ws::Frame> for Frame<'_> {
Payload::Owned(frame.payload.to_vec()),
),
Pong => Self::pong(Payload::Owned(frame.payload.to_vec())),
}
})
}
}
@ -66,6 +67,6 @@ impl<S: AsyncRead + Unpin> crate::ws::WebSocketRead for FragmentCollectorRead<S>
impl<S: AsyncWrite + Unpin> crate::ws::WebSocketWrite for WebSocketWrite<S> {
async fn wisp_write_frame(&mut self, frame: crate::ws::Frame) -> Result<(), crate::WispError> {
self.write_frame(frame.into()).await.map_err(|e| e.into())
self.write_frame(frame.try_into()?).await.map_err(|e| e.into())
}
}