Copy bytes from WebSocket binary frames into a local TCP writer.
(
mut ws_source: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
mut tcp_write: tokio::io::WriteHalf<TcpStream>,
)
| 215 | |
| 216 | /// Copy bytes from WebSocket binary frames into a local TCP writer. |
| 217 | async fn copy_ws_to_tcp( |
| 218 | mut ws_source: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>, |
| 219 | mut tcp_write: tokio::io::WriteHalf<TcpStream>, |
| 220 | ) { |
| 221 | while let Some(msg) = ws_source.next().await { |
| 222 | match msg { |
| 223 | Ok(Message::Binary(data)) => { |
| 224 | if tcp_write.write_all(&data).await.is_err() { |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | Ok(Message::Close(_)) => break, |
| 229 | Ok(Message::Ping(_) | Message::Pong(_) | Message::Frame(_)) => { |
| 230 | // Handled automatically by tungstenite. |
| 231 | } |
| 232 | Ok(Message::Text(text)) => { |
| 233 | // Some proxies send text frames — treat as binary. |
| 234 | if tcp_write.write_all(text.as_bytes()).await.is_err() { |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | Err(e) => { |
| 239 | debug!(error = %e, "ws read error"); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | let _ = tcp_write.shutdown().await; |
| 245 | } |
no test coverage detected