Advertisement
NLinker

Async postgres client

Nov 7th, 2019
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.00 KB | None | 0 0
  1. // [package]
  2. // name = "websocket-test"
  3. // version = "0.1.0"
  4. // authors = []
  5. // edition = "2018"
  6.  
  7. // [dependencies]
  8. // websocket-lowlevel = {
  9. //      git = "https://github.com/sdroege/rust-websocket.git",
  10. //      rev = "tokio-0.2",
  11. //      default-features = false,
  12. //      features = ["async"]
  13. //  }
  14. // futures-preview = { version = "0.3.0-alpha.19" }
  15. // tokio = { version = "0.2.0-alpha.6" }
  16. // tokio-io = { version = "0.2.0-alpha.6" }
  17. // hyper = { version = "0.13.0-alpha.4" }
  18. // hyper-tls = { version = "0.4.0-alpha.4" }
  19.  
  20. use tokio::codec::Decoder;
  21. use tokio::prelude::*;
  22.  
  23. fn main() {
  24.     let https = hyper_tls::HttpsConnector::new().unwrap();
  25.     let client = hyper::Client::builder().build::<_, hyper::Body>(https);
  26.  
  27.     let f = async {
  28.         let key = websocket_lowlevel::header::WebSocketKey::new();
  29.         let req = hyper::Request::builder()
  30.             .uri("https://echo.websocket.org")
  31.             .header(hyper::header::HOST, "echo.websocket.org")
  32.             .header(hyper::header::UPGRADE, "websocket")
  33.             .header(hyper::header::CONNECTION, "Upgrade")
  34.             .header("Sec-WebSocket-Version", "13")
  35.             .header("Sec-WebSocket-Key", key.serialize())
  36.             .body(hyper::Body::empty())
  37.             .unwrap();
  38.  
  39.         println!("req: {:?}", req);
  40.         let res = client.request(req).await;
  41.         println!("res: {:?}", res);
  42.         let res = res.expect("Failed HTTP request");
  43.  
  44.         if res.status() != hyper::StatusCode::SWITCHING_PROTOCOLS {
  45.             panic!("Not switching protocols: {}", res.status());
  46.         }
  47.  
  48.         let headers = res.headers();
  49.         let accept = headers
  50.             .get("Sec-WebSocket-Accept")
  51.             .and_then(|h| h.to_str().ok());
  52.         if accept.is_none() {
  53.             panic!("No Accept header");
  54.         }
  55.         let accept = accept.unwrap();
  56.  
  57.         let expected_accept = websocket_lowlevel::header::WebSocketAccept::new(&key);
  58.         if expected_accept.serialize() != accept {
  59.             panic!(
  60.                 "Accept header wrong: excepted {}, got {}",
  61.                 expected_accept.serialize(),
  62.                 accept
  63.             );
  64.         }
  65.  
  66.         let (mut w, r) = match res.into_body().on_upgrade().await {
  67.             Ok(upgrade) => {
  68.                 let framed = websocket_lowlevel::codec::ws::MessageCodec::default(
  69.                     websocket_lowlevel::codec::ws::Context::Client,
  70.                 )
  71.                 .framed(upgrade);
  72.  
  73.                 framed.split()
  74.             }
  75.             Err(err) => {
  76.                 panic!("Failed to upgrade: {:?}", err);
  77.             }
  78.         };
  79.  
  80.         let res = w
  81.             .send(websocket_lowlevel::message::OwnedMessage::Text(
  82.                 "blablabla".into(),
  83.             ))
  84.             .await;
  85.         res.expect("sending failed");
  86.  
  87.         let (m, r) = r.into_future().await;
  88.         let m = m.unwrap();
  89.         println!("msg {:?}", m);
  90.     };
  91.  
  92.     tokio::runtime::Builder::new().build().unwrap().block_on(f);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement