1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-30 08:24:28 +02:00

use ByteString as container for websocket text message (#1864)

This commit is contained in:
Rob Ede
2021-01-04 11:27:32 +00:00
committed by GitHub
parent 36aee18c64
commit 7d632d0b7b
13 changed files with 131 additions and 102 deletions

View File

@ -1,9 +1,10 @@
//! Websocket integration
use std::collections::VecDeque;
//! Websocket integration.
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{collections::VecDeque, convert::TryFrom};
use actix::dev::{
AsyncContextParts, ContextFut, ContextParts, Envelope, Mailbox, StreamHandler,
@ -24,10 +25,11 @@ use actix_web::error::{Error, PayloadError};
use actix_web::http::{header, Method, StatusCode};
use actix_web::{HttpRequest, HttpResponse};
use bytes::{Bytes, BytesMut};
use bytestring::ByteString;
use futures_core::Stream;
use tokio::sync::oneshot::Sender;
/// Do websocket handshake and start ws actor.
/// Perform WebSocket handshake and start actor.
pub fn start<A, T>(actor: A, req: &HttpRequest, stream: T) -> Result<HttpResponse, Error>
where
A: Actor<Context = WebsocketContext<A>>
@ -38,7 +40,7 @@ where
Ok(res.streaming(WebsocketContext::create(actor, stream)))
}
/// Do websocket handshake and start ws actor.
/// Perform WebSocket handshake and start actor.
///
/// `req` is an HTTP Request that should be requesting a websocket protocol
/// change. `stream` should be a `Bytes` stream (such as
@ -338,13 +340,13 @@ where
/// Send text frame
#[inline]
pub fn text<T: Into<String>>(&mut self, text: T) {
pub fn text(&mut self, text: impl Into<ByteString>) {
self.write_raw(Message::Text(text.into()));
}
/// Send binary frame
#[inline]
pub fn binary<B: Into<Bytes>>(&mut self, data: B) {
pub fn binary(&mut self, data: impl Into<Bytes>) {
self.write_raw(Message::Binary(data.into()));
}
@ -528,16 +530,14 @@ where
}
Some(frm) => {
let msg = match frm {
Frame::Text(data) => Message::Text(
std::str::from_utf8(&data)
.map_err(|e| {
ProtocolError::Io(io::Error::new(
io::ErrorKind::Other,
format!("{}", e),
))
})?
.to_string(),
),
Frame::Text(data) => {
Message::Text(ByteString::try_from(data).map_err(|e| {
ProtocolError::Io(io::Error::new(
io::ErrorKind::Other,
format!("{}", e),
))
})?)
}
Frame::Binary(data) => Message::Binary(data),
Frame::Ping(s) => Message::Ping(s),
Frame::Pong(s) => Message::Pong(s),