This commit is contained in:
mii443
2024-08-31 22:36:02 +09:00
parent 8684435276
commit ac9c39d1a6
2 changed files with 12 additions and 20 deletions

View File

@@ -26,7 +26,7 @@ async fn main() -> Result<()> {
async fn client_a() -> Result<()> { async fn client_a() -> Result<()> {
let mut p2p = P2P::new(None).await?; 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}"); println!("{code}");
@@ -34,45 +34,36 @@ async fn client_a() -> Result<()> {
println!("Connected!"); println!("Connected!");
tokio::spawn({ chat(p2p).await
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?;
}
} }
async fn client_b(code: &str) -> Result<()> { async fn client_b(code: &str) -> Result<()> {
let mut p2p = P2P::new(None).await?; 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; p2p.wait_open().await;
println!("Connected!"); println!("Connected!");
chat(p2p).await
}
async fn chat(mut p2p: P2P) -> Result<()> {
tokio::spawn({ tokio::spawn({
let receive = p2p.receive_data.clone(); let receive = p2p.receive_data.clone();
async move { async move {
let mut receive = receive.lock().await; let mut receive = receive.lock().await;
while let Some(data) = receive.recv().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 { loop {
let line = read_line(); let line = read_line();
p2p.send_text(&line).await?; p2p.send_text(&format!("{line}\n")).await?;
} }
} }

View File

@@ -94,6 +94,7 @@ impl P2P {
let data = String::from_utf8_lossy(&chunk); let data = String::from_utf8_lossy(&chunk);
for line in data.lines() { for line in data.lines() {
println!("{line}");
if line.starts_with("data:") { if line.starts_with("data:") {
let value = line.trim_start_matches("data:").trim(); let value = line.trim_start_matches("data:").trim();
if connection_code_received { if connection_code_received {