Skip to content

Commit

Permalink
change example
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoremWinbringer committed Jan 3, 2019
1 parent 6392c2f commit d03c902
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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<Vec<u8>>,
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<u8> {
b"Pong".to_vec()
fn draw(&mut self, delta_time: Duration) -> Vec<u8> {
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();
}
```
Expand Down
14 changes: 10 additions & 4 deletions examples/ping_pong_client.rs
Original file line number Diff line number Diff line change
@@ -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()))
Expand Down

0 comments on commit d03c902

Please sign in to comment.