Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions websocket-chat/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
//! room through `ChatServer`.

use actix::prelude::*;
use rand::{self, Rng, ThreadRng};
use std::cell::RefCell;
use rand::{self, rngs::ThreadRng, Rng};
use std::collections::{HashMap, HashSet};

/// Chat server sends this messages to session
Expand Down Expand Up @@ -58,7 +57,7 @@ pub struct Join {
pub struct ChatServer {
sessions: HashMap<usize, Recipient<Message>>,
rooms: HashMap<String, HashSet<usize>>,
rng: RefCell<ThreadRng>,
rng: ThreadRng,
}

impl Default for ChatServer {
Expand All @@ -70,7 +69,7 @@ impl Default for ChatServer {
ChatServer {
sessions: HashMap::new(),
rooms: rooms,
rng: RefCell::new(rand::thread_rng()),
rng: rand::thread_rng(),
}
}
}
Expand Down Expand Up @@ -110,7 +109,7 @@ impl Handler<Connect> for ChatServer {
self.send_message(&"Main".to_owned(), "Someone joined", 0);

// register session with random id
let id = self.rng.borrow_mut().gen::<usize>();
let id = self.rng.gen::<usize>();
self.sessions.insert(id, msg.addr);

// auto join session to Main room
Expand Down
9 changes: 4 additions & 5 deletions websocket-tcp-chat/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
//! room through `ChatServer`.

use actix::prelude::*;
use rand::{self, Rng, ThreadRng};
use std::cell::RefCell;
use rand::{self, rngs::ThreadRng, Rng};
use std::collections::{HashMap, HashSet};

use session;
Expand Down Expand Up @@ -56,7 +55,7 @@ pub struct Join {
pub struct ChatServer {
sessions: HashMap<usize, Recipient<session::Message>>,
rooms: HashMap<String, HashSet<usize>>,
rng: RefCell<ThreadRng>,
rng: ThreadRng,
}

impl Default for ChatServer {
Expand All @@ -68,7 +67,7 @@ impl Default for ChatServer {
ChatServer {
sessions: HashMap::new(),
rooms: rooms,
rng: RefCell::new(rand::thread_rng()),
rng: rand::thread_rng(),
}
}
}
Expand Down Expand Up @@ -108,7 +107,7 @@ impl Handler<Connect> for ChatServer {
self.send_message(&"Main".to_owned(), "Someone joined", 0);

// register session with random id
let id = self.rng.borrow_mut().gen::<usize>();
let id = self.rng.gen::<usize>();
self.sessions.insert(id, msg.addr);

// auto join session to Main room
Expand Down