1 unstable release
Uses old Rust 2015
0.0.1 | Mar 20, 2015 |
---|
#11 in #rooms
10KB
141 lines
TCP server implementation suitable for chat rooms and games.
The lobby
crate is designed to provide a convenient way to spin up a server
which automatically keeps track of connected clients and provides facilities
for messaging them and polling them for data.
Each client is represented using a unique integer id, and id's are recycled as clients disconnect from the server.
Here's how you spin up a lobby
server and poll the clients for data:
extern crate lobby;
use lobby::{Lobby, ScanResult};
let server = Lobby::new("127.0.0.1:8080").unwrap();
loop {
server.scan(|id, result| {
let name = server.name(id).unwrap();
match result {
ScanResult::Connected => println!("{} has connected.", name),
ScanResult::Data(data) => println!("{} sent {} bytes of data.", name, data.len()),
ScanResult::IoError(err) => println!("{} ran into an IO error: {}", name, err),
ScanResult::Disconnected => println!("{} has disconnected.", name),
}
});
}
Clients can then connect to the server using TcpStream::connect()
. The first
thing a client should do after establishing the connection is send a UTF-8 encoded
name followed by a 0 byte to indicate its termination. After that, all further
data sent will be queued up to be scanned by the server.