diff --git a/actix-http/src/ws/codec.rs b/actix-http/src/ws/codec.rs index ad599ffa..9891bfa6 100644 --- a/actix-http/src/ws/codec.rs +++ b/actix-http/src/ws/codec.rs @@ -37,7 +37,7 @@ pub enum Frame { Close(Option), } -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] /// WebSockets protocol codec pub struct Codec { max_size: usize, diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index 0ef3c916..16f475a7 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -177,9 +177,26 @@ where inner: ContextParts::new(mb.sender_producer()), messages: VecDeque::new(), }; - ctx.add_stream(WsStream::new(stream)); + ctx.add_stream(WsStream::new(stream, Codec::new())); - WebsocketContextFut::new(ctx, actor, mb) + WebsocketContextFut::new(ctx, actor, mb, Codec::new()) + } + + #[inline] + /// Create a new Websocket context from a request, an actor, and a codec + pub fn with_codec(actor: A, stream: S, codec: Codec) -> impl Stream + where + A: StreamHandler, + S: Stream + 'static, + { + let mb = Mailbox::default(); + let mut ctx = WebsocketContext { + inner: ContextParts::new(mb.sender_producer()), + messages: VecDeque::new(), + }; + ctx.add_stream(WsStream::new(stream, codec)); + + WebsocketContextFut::new(ctx, actor, mb, codec) } /// Create a new Websocket context @@ -197,11 +214,11 @@ where inner: ContextParts::new(mb.sender_producer()), messages: VecDeque::new(), }; - ctx.add_stream(WsStream::new(stream)); + ctx.add_stream(WsStream::new(stream, Codec::new())); let act = f(&mut ctx); - WebsocketContextFut::new(ctx, act, mb) + WebsocketContextFut::new(ctx, act, mb, Codec::new()) } } @@ -288,11 +305,11 @@ impl WebsocketContextFut where A: Actor>, { - fn new(ctx: WebsocketContext, act: A, mailbox: Mailbox) -> Self { + fn new(ctx: WebsocketContext, act: A, mailbox: Mailbox, codec: Codec) -> Self { let fut = ContextFut::new(ctx, act, mailbox); WebsocketContextFut { fut, - encoder: Codec::new(), + encoder: codec, buf: BytesMut::new(), closed: false, } @@ -353,10 +370,10 @@ impl WsStream where S: Stream, { - fn new(stream: S) -> Self { + fn new(stream: S, codec: Codec) -> Self { Self { stream, - decoder: Codec::new(), + decoder: codec, buf: BytesMut::new(), closed: false, }