mirror of https://github.com/fafhrd91/actix-web
move `create_with_codec_addr` to WsResponseBuilder
This commit is contained in:
parent
f11e2893cc
commit
ad78ca9f35
|
@ -124,6 +124,32 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new Websocket context from a request, an actor, and a codec.
|
||||||
|
///
|
||||||
|
/// Returns a pair, where the first item is an addr for the created actor,
|
||||||
|
/// and the second item is a stream intended to be set as part of the
|
||||||
|
/// response via `HttpResponseBuilder::streaming()`.
|
||||||
|
fn create_with_codec_addr<S>(
|
||||||
|
actor: A,
|
||||||
|
stream: S,
|
||||||
|
codec: Codec,
|
||||||
|
) -> (Addr<A>, impl Stream<Item = Result<Bytes, Error>>)
|
||||||
|
where
|
||||||
|
A: StreamHandler<Result<Message, ProtocolError>>,
|
||||||
|
S: Stream<Item = Result<Bytes, PayloadError>> + '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));
|
||||||
|
|
||||||
|
let addr = ctx.address();
|
||||||
|
|
||||||
|
(addr, WebsocketContextFut::new(ctx, actor, mb, codec))
|
||||||
|
}
|
||||||
|
|
||||||
/// Perform WebSocket handshake and start actor.
|
/// Perform WebSocket handshake and start actor.
|
||||||
///
|
///
|
||||||
/// `req` is an [`HttpRequest`] that should be requesting a websocket
|
/// `req` is an [`HttpRequest`] that should be requesting a websocket
|
||||||
|
@ -168,11 +194,8 @@ where
|
||||||
|
|
||||||
match self.codec {
|
match self.codec {
|
||||||
Some(codec) => {
|
Some(codec) => {
|
||||||
let (addr, out_stream) = WebsocketContext::create_with_codec_addr(
|
let (addr, out_stream) =
|
||||||
self.actor,
|
Self::create_with_codec_addr(self.actor, self.stream, codec);
|
||||||
self.stream,
|
|
||||||
codec,
|
|
||||||
);
|
|
||||||
Ok((addr, res.streaming(out_stream)))
|
Ok((addr, res.streaming(out_stream)))
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
@ -434,33 +457,6 @@ where
|
||||||
(addr, WebsocketContextFut::new(ctx, actor, mb, Codec::new()))
|
(addr, WebsocketContextFut::new(ctx, actor, mb, Codec::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Create a new Websocket context from a request, an actor, and a codec.
|
|
||||||
///
|
|
||||||
/// Returns a pair, where the first item is an addr for the created actor,
|
|
||||||
/// and the second item is a stream intended to be set as part of the
|
|
||||||
/// response via `HttpResponseBuilder::streaming()`.
|
|
||||||
pub fn create_with_codec_addr<S>(
|
|
||||||
actor: A,
|
|
||||||
stream: S,
|
|
||||||
codec: Codec,
|
|
||||||
) -> (Addr<A>, impl Stream<Item = Result<Bytes, Error>>)
|
|
||||||
where
|
|
||||||
A: StreamHandler<Result<Message, ProtocolError>>,
|
|
||||||
S: Stream<Item = Result<Bytes, PayloadError>> + '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));
|
|
||||||
|
|
||||||
let addr = ctx.address();
|
|
||||||
|
|
||||||
(addr, WebsocketContextFut::new(ctx, actor, mb, codec))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Create a new Websocket context from a request, an actor, and a codec
|
/// Create a new Websocket context from a request, an actor, and a codec
|
||||||
pub fn with_codec<S>(
|
pub fn with_codec<S>(
|
||||||
|
|
Loading…
Reference in New Issue