Skip to content

Commit caa815b

Browse files
nanoqshsdroege
authored andcommitted
Make WebSocketSender methods take &mut self
1 parent 782ad71 commit caa815b

File tree

3 files changed

+4
-54
lines changed

3 files changed

+4
-54
lines changed

examples/interval-server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn accept_connection(peer: SocketAddr, stream: TcpStream) {
2222
async fn handle_connection(peer: SocketAddr, stream: TcpStream) -> Result<()> {
2323
let ws_stream = accept_async(stream).await.expect("Failed to accept");
2424
info!("New WebSocket connection: {}", peer);
25-
let (ws_sender, mut ws_receiver) = ws_stream.split();
25+
let (mut ws_sender, mut ws_receiver) = ws_stream.split();
2626
let mut interval = async_std::stream::interval(Duration::from_millis(1000));
2727

2828
// Echo incoming WebSocket messages and send a message periodically every second.

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ pub struct WebSocketSender<S> {
609609

610610
impl<S> WebSocketSender<S> {
611611
/// Send a message via [websocket](WebSocketStream).
612-
pub async fn send(&self, msg: Message) -> Result<(), WsError>
612+
pub async fn send(&mut self, msg: Message) -> Result<(), WsError>
613613
where
614614
S: AsyncRead + AsyncWrite + Unpin,
615615
{
@@ -621,7 +621,7 @@ impl<S> WebSocketSender<S> {
621621
}
622622

623623
/// Close the underlying [websocket](WebSocketStream).
624-
pub async fn close(&self, msg: Option<CloseFrame>) -> Result<(), WsError>
624+
pub async fn close(&mut self, msg: Option<CloseFrame>) -> Result<(), WsError>
625625
where
626626
S: AsyncRead + AsyncWrite + Unpin,
627627
{

tests/communication.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async fn split_communication() {
101101
.await
102102
.expect("Client failed to connect");
103103

104-
let (tx, rx) = stream.split();
104+
let (mut tx, rx) = stream.split();
105105

106106
for i in 1..10 {
107107
info!("Sending message");
@@ -120,53 +120,3 @@ async fn split_communication() {
120120
assert!(rx.is_pair_of(&tx));
121121
WebSocketStream::reunite(tx, rx).expect("Failed to reunite the stream");
122122
}
123-
124-
#[async_std::test]
125-
async fn concurrent_send() {
126-
let _ = env_logger::try_init();
127-
128-
let (con_tx, con_rx) = futures::channel::oneshot::channel();
129-
let (msg_tx, msg_rx) = futures::channel::oneshot::channel();
130-
131-
let f = async move {
132-
let listener = TcpListener::bind("127.0.0.1:12347").await.unwrap();
133-
info!("Server ready");
134-
con_tx.send(()).unwrap();
135-
info!("Waiting on next connection");
136-
let (connection, _) = listener.accept().await.expect("No connections to accept");
137-
let stream = accept_async(connection).await;
138-
let stream = stream.expect("Failed to handshake with connection");
139-
run_connection(stream, msg_tx).await;
140-
};
141-
142-
task::spawn(f);
143-
144-
info!("Waiting for server to be ready");
145-
146-
con_rx.await.expect("Server not ready");
147-
let tcp = TcpStream::connect("127.0.0.1:12347")
148-
.await
149-
.expect("Failed to connect");
150-
let url = url::Url::parse("ws://localhost:12347/").unwrap();
151-
let (stream, _) = client_async(url, tcp)
152-
.await
153-
.expect("Client failed to connect");
154-
155-
let (tx, _rx) = stream.split();
156-
157-
// the `WebSocketSender::send` takes a shared `&self`, so you can call it concurrently.
158-
// this test case checks that it works
159-
let results = futures::future::join_all((1..10).map(|i| {
160-
info!("Sending message");
161-
tx.send(Message::text(format!("{}", i)))
162-
}))
163-
.await;
164-
165-
assert!(results.iter().all(Result::is_ok));
166-
167-
tx.close(None).await.expect("Failed to close");
168-
169-
info!("Waiting for response messages");
170-
let messages = msg_rx.await.expect("Failed to receive messages");
171-
assert_eq!(messages.len(), 10);
172-
}

0 commit comments

Comments
 (0)