Created
October 5, 2016 16:52
-
-
Save Mazyod/8f6807e2c859b3457eaa4bcf24d5d703 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate redis; | |
use redis::Commands; | |
use std::net::{TcpListener, TcpStream}; | |
use std::thread; | |
use std::io::Read; | |
fn handle_client(mut stream: TcpStream) -> redis::RedisResult<()> { | |
let client = try!(redis::Client::open("redis://redis/")); | |
let con = try!(client.get_connection()); | |
let mut buffer = [0; 2048]; | |
let _ = stream.read(&mut buffer); | |
let s = format!("{}, Rust!", String::from_utf8_lossy(&buffer).trim()); | |
println!("Received string: {} ..", s); | |
let _ = try!(con.set("rusty_key", s)); | |
Ok(()) | |
} | |
fn main() { | |
let listener = TcpListener::bind("0.0.0.0:3500").unwrap(); | |
// accept connections and process them, spawning a new thread for each one | |
for stream in listener.incoming() { | |
match stream { | |
Ok(stream) => { | |
thread::spawn(move|| { | |
println!("Client connected!"); | |
handle_client(stream); | |
}); | |
} | |
Err(e) => { | |
println!("client errored!"); | |
} | |
} | |
} | |
// close the socket server | |
drop(listener); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment