rust #10

Merged
peperunas merged 127 commits from rust into master 2021-02-18 09:42:16 +00:00
5 changed files with 1673 additions and 1 deletions
Showing only changes of commit 6b97847882 - Show all commits

1622
rustybot/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
rustybot/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "rustybot"
version = "0.1.0"
authors = ["Giulio De Pasquale <depasquale@giugl.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bitfinex = { path= "/home/giulio/gitstuff/bitfinex-rs" }
tokio = { version = "0.3", features=["full"]}
tokio-tungstenite = "*"
futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] }

35
rustybot/src/main.rs Normal file
View File

@ -0,0 +1,35 @@
use std::env;
use std::net::SocketAddr;
use futures_util::StreamExt;
use tokio::net::{TcpListener, TcpStream};
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
async fn accept_connection(stream: TcpStream, addr: SocketAddr) -> Result<(), BoxError> {
println!("Peer address: {}", addr);
let ws_stream = tokio_tungstenite::accept_async(stream)
.await
.err(format!("Error during WS handshake.").into());
println!("New WebSocket connection: {}", addr);
let (write, read) = ws_stream.split();
read.forward(write).await.expect("Failed to forward message")
}
#[tokio::main]
async fn main() -> Result<(), BoxError> {
// Create the event loop and TCP listener we'll accept connections on.
let addr = env::args().nth(1).unwrap_or_else(|| "127.0.0.1:8080".to_string());
let try_socket = TcpListener::bind(&addr).await;
let mut listener = try_socket.expect("Failed to bind");
while let Ok((stream, addr)) = listener.accept().await {
tokio::spawn(accept_connection(stream, addr));
}
Ok(())
}

File diff suppressed because one or more lines are too long

View File

@ -10,4 +10,6 @@ socket.on("connect", function () {
console.log("Connected!")
})
const ws = new WebSocket('ws://localhost:8080');
ReactDOM.render(<App/>, document.getElementById("root"));