From d03c902245d4b4a09532ab7660ad23fb68cb8bbf Mon Sep 17 00:00:00 2001 From: VictoremWinbringer Date: Thu, 3 Jan 2019 15:26:28 +0300 Subject: [PATCH] change example --- README.md | 43 +++++++++++++++++++++++------------- examples/ping_pong_client.rs | 14 ++++++++---- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c1d41b6..e14fd05 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,20 @@ victorem = "*" ### Client ```rust use victorem; +use std::time::{Duration, Instant}; + fn main() { let mut client = victorem::ClientSocket::new("1111", "127.0.0.1:2222").unwrap(); + let mut id: u32 = 0; + let mut timer = Instant::now(); + let period = Duration::from_millis(100); loop { - let _ = client - .send(b"Ping!".to_vec()) - .map_err(|e| println!("{:#?}", e)); + if timer.elapsed() > period { + timer = Instant::now(); + id += 1; + let _ = client + .send(format!("Ping {}", id).into_bytes()); + } let _ = client .recv() .map(|v| String::from_utf8(v).unwrap_or(String::new())) @@ -28,34 +36,39 @@ use victorem; use std::net::SocketAddr; use std::time::Duration; -struct PingPongGame {} +struct PingPongGame { + id: u32, +} impl victorem::Game for PingPongGame { fn handle_command( &mut self, - _delta_time: Duration, + delta_time: Duration, commands: Vec>, from: SocketAddr, ) -> victorem::ContinueRunning { for v in commands { - if v.len() > 0 { - println!( - "From Client: {} {}", - from, - String::from_utf8(v).unwrap_or(String::new()), - ); - } + println!( + "From Client: {:?} {} {}", + delta_time, + from, + String::from_utf8(v).unwrap_or(String::new()), + ); } true } - fn draw(&mut self, _delta_time: Duration) -> Vec { - b"Pong".to_vec() + fn draw(&mut self, delta_time: Duration) -> Vec { + self.id += 1; + format!("Pong {} {:?}", self.id, delta_time).into_bytes() } } fn main() { - let mut server = victorem::GameServer::new(PingPongGame {}, "2222").unwrap(); + let mut server = victorem::GameServer::new( + PingPongGame { id: 0 }, + "2222", + ).unwrap(); server.run(); } ``` diff --git a/examples/ping_pong_client.rs b/examples/ping_pong_client.rs index 7bbf6c4..4a84eaa 100644 --- a/examples/ping_pong_client.rs +++ b/examples/ping_pong_client.rs @@ -1,13 +1,19 @@ extern crate victorem; +use std::time::{Duration, Instant}; + fn main() { let mut client = victorem::ClientSocket::new("1111", "127.0.0.1:2222").unwrap(); let mut id: u32 = 0; + let mut timer = Instant::now(); + let period = Duration::from_millis(100); loop { - id += 1; - let _ = client - .send(format!("Ping {}", id).into_bytes()) - .map_err(|e| println!("{:#?}", e)); + if timer.elapsed() > period { + timer = Instant::now(); + id += 1; + let _ = client + .send(format!("Ping {}", id).into_bytes()); + } let _ = client .recv() .map(|v| String::from_utf8(v).unwrap_or(String::new()))