mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 06:57:43 +02:00
use ByteString as container for websocket text message (#1864)
This commit is contained in:
@ -3,8 +3,10 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
* Update `pin-project` to `1.0`.
|
||||
* Update `bytes` to `1.0`. [#1813]
|
||||
* `WebsocketContext::text` now takes an `Into<bytestring::ByteString>`. [#1864]
|
||||
|
||||
[#1813]: https://github.com/actix/actix-web/pull/1813
|
||||
[#1864]: https://github.com/actix/actix-web/pull/1864
|
||||
|
||||
## 3.0.0 - 2020-09-11
|
||||
* No significant changes from `3.0.0-beta.2`.
|
||||
|
@ -22,6 +22,7 @@ actix-http = "2.0.0"
|
||||
actix-web = { version = "3.0.0", default-features = false }
|
||||
|
||||
bytes = "1"
|
||||
bytestring = "1"
|
||||
futures-core = { version = "0.3.7", default-features = false }
|
||||
pin-project = "1.0.0"
|
||||
tokio = { version = "1", features = ["sync"] }
|
||||
|
@ -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),
|
||||
|
@ -38,10 +38,7 @@ async fn test_simple() {
|
||||
|
||||
// client service
|
||||
let mut framed = srv.ws().await.unwrap();
|
||||
framed
|
||||
.send(ws::Message::Text("text".to_string()))
|
||||
.await
|
||||
.unwrap();
|
||||
framed.send(ws::Message::Text("text".into())).await.unwrap();
|
||||
|
||||
let item = framed.next().await.unwrap().unwrap();
|
||||
assert_eq!(item, ws::Frame::Text(Bytes::from_static(b"text")));
|
||||
|
Reference in New Issue
Block a user