This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::convert::Infallible; | |
use std::net::SocketAddr; | |
use hyper::{header, upgrade, StatusCode, Body, Request, Response, Server, server::conn::AddrStream}; | |
use hyper::service::{make_service_fn, service_fn}; | |
use tokio_tungstenite::WebSocketStream; | |
use tungstenite::{handshake, Error}; | |
use futures::stream::StreamExt; | |
async fn handle_request(mut request: Request<Body>, remote_addr: SocketAddr) -> Result<Response<Body>, Infallible> { | |
match (request.uri().path(), request.headers().contains_key(header::UPGRADE)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use tungstenite::{handshake, Error}; | |
... | |
match handshake::server::create_response_with_body(&request, || Body::empty()) { | |
Ok(response) => { | |
//handle websocket connection HERE | |
... | |
//return the response to the handshake request | |
response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use hyper::upgrade; | |
use tokio_tungstenite::WebSocketStream; | |
use futures::stream::StreamExt; | |
... | |
tokio::spawn(async move { | |
//using the hyper feature of upgrading a connection | |
match upgrade::on(&mut request).await { | |
//if successfully upgraded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use hyper::header; | |
... | |
async fn handle_request(mut request: Request<Body>, remote_addr: SocketAddr) -> Result<Response<Body>, Infallible> { | |
match (request.uri().path(), request.headers().contains_key(header::UPGRADE)) { | |
//if the request is ws_echo and the request headers contains an Upgrade key | |
("/ws_echo", true) => { | |
//handle websocket HERE | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::convert::Infallible; | |
use std::net::SocketAddr; | |
use hyper::{Body, Request, Response, Server, server::conn::AddrStream}; | |
use hyper::service::{make_service_fn, service_fn}; | |
async fn handle_request(request: Request<Body>, remote_addr: SocketAddr) -> Result<Response<Body>, Infallible> { | |
Ok(Response::new(Body::from(format!("Hello there connection {}\n", remote_addr)))) | |
} | |
#[tokio::main(flavor = "current_thread")] |