mirror of
https://github.com/mii443/rs-easy-p2p.git
synced 2025-12-03 03:08:21 +00:00
fix bin
This commit is contained in:
31
src/bin.rs
31
src/bin.rs
@@ -26,7 +26,7 @@ async fn main() -> Result<()> {
|
||||
async fn client_a() -> Result<()> {
|
||||
let mut p2p = P2P::new(None).await?;
|
||||
|
||||
let code = p2p.create_code("http://localhost:3000").await?;
|
||||
let code = p2p.create_code("https://signaling.mii.dev").await?;
|
||||
|
||||
println!("{code}");
|
||||
|
||||
@@ -34,45 +34,36 @@ async fn client_a() -> Result<()> {
|
||||
|
||||
println!("Connected!");
|
||||
|
||||
tokio::spawn({
|
||||
let receive = p2p.receive_data.clone();
|
||||
|
||||
async move {
|
||||
let mut receive = receive.lock().await;
|
||||
while let Some(data) = receive.recv().await {
|
||||
println!("{}", String::from_utf8(data.to_vec()).unwrap());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
let line = read_line();
|
||||
p2p.send_text(&line).await?;
|
||||
}
|
||||
chat(p2p).await
|
||||
}
|
||||
|
||||
async fn client_b(code: &str) -> Result<()> {
|
||||
let mut p2p = P2P::new(None).await?;
|
||||
|
||||
p2p.connect_with_code("http://localhost:3000", code).await?;
|
||||
p2p.connect_with_code("https://signaling.mii.dev", code).await?;
|
||||
|
||||
p2p.wait_open().await;
|
||||
|
||||
println!("Connected!");
|
||||
|
||||
chat(p2p).await
|
||||
}
|
||||
|
||||
async fn chat(mut p2p: P2P) -> Result<()> {
|
||||
tokio::spawn({
|
||||
let receive = p2p.receive_data.clone();
|
||||
|
||||
async move {
|
||||
let mut receive = receive.lock().await;
|
||||
while let Some(data) = receive.recv().await {
|
||||
println!("Received: {}", String::from_utf8(data.to_vec()).unwrap());
|
||||
print!("Received: {}\n", String::from_utf8(data.to_vec()).unwrap());
|
||||
std::io::stdout().flush().unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
let line = read_line();
|
||||
p2p.send_text(&line).await?;
|
||||
p2p.send_text(&format!("{line}\n")).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,7 @@ impl P2P {
|
||||
let data = String::from_utf8_lossy(&chunk);
|
||||
|
||||
for line in data.lines() {
|
||||
println!("{line}");
|
||||
if line.starts_with("data:") {
|
||||
let value = line.trim_start_matches("data:").trim();
|
||||
if connection_code_received {
|
||||
|
||||
Reference in New Issue
Block a user