Function socket_flow::handshake::accept_async
source ยท pub async fn accept_async(stream: TcpStream) -> ResultExpand description
Used for accepting websocket connections as a server.
It basically does the first step of verifying the client key in the request
going to the second step, which is sending the accept response,
finally creating the connection, and returning a WSConnection
Examples found in repository?
examples/echo_server.rs (line 8)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
async fn handle_connection(_: SocketAddr, stream: TcpStream) {
match accept_async(stream).await {
Ok(mut ws_connection) => {
while let Some(result) = ws_connection.next().await {
match result {
Ok(message) => {
if ws_connection.send_message(message).await.is_err() {
error!("Failed to send message");
break;
}
}
Err(e) => {
error!("Received error from the stream: {}", e);
break;
}
}
}
}
Err(err) => error!("Error when performing handshake: {}", err),
}
}