removed unecassary arc from sender type
This commit is contained in:
parent
d8e30fcf55
commit
d04a353e52
1 changed files with 8 additions and 12 deletions
20
src/main.rs
20
src/main.rs
|
@ -7,7 +7,7 @@ use tokio_tungstenite::{accept_async, tungstenite::Message};
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut connection_count: usize = 0;
|
let mut connection_count: usize = 0;
|
||||||
let senders = Arc::new(Mutex::new(HashMap::<usize, Sender<Arc<Message>>>::new()));
|
let senders = Arc::new(Mutex::new(HashMap::<usize, Sender<Message>>::new()));
|
||||||
|
|
||||||
let server = TcpListener::bind("0.0.0.0:9001").await.unwrap();
|
let server = TcpListener::bind("0.0.0.0:9001").await.unwrap();
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ async fn main() {
|
||||||
println!("Connection Received from ip: {}, port: {}", socket.ip(), socket.port());
|
println!("Connection Received from ip: {}, port: {}", socket.ip(), socket.port());
|
||||||
|
|
||||||
let t_senders = Arc::clone(&senders);
|
let t_senders = Arc::clone(&senders);
|
||||||
let (sender, mut receiver): (Sender<Arc<Message>>, Receiver<Arc<Message>>) = channel(100);
|
let (sender, mut receiver): (Sender<Message>, Receiver<Message>) = channel(100);
|
||||||
{
|
{
|
||||||
let mut sender_lock = t_senders.lock().await;
|
let mut sender_lock = t_senders.lock().await;
|
||||||
connection_count += 1;
|
connection_count += 1;
|
||||||
|
@ -33,22 +33,19 @@ async fn main() {
|
||||||
while let Some(res) = websocket_read.next().await {
|
while let Some(res) = websocket_read.next().await {
|
||||||
match res {
|
match res {
|
||||||
Ok(msg) => {
|
Ok(msg) => {
|
||||||
let arc_msg = Arc::new(msg);
|
if msg.is_close() {
|
||||||
if arc_msg.is_close() {
|
|
||||||
let sender_lock = t_senders.lock().await;
|
let sender_lock = t_senders.lock().await;
|
||||||
let arc_msg_clone = Arc::clone(&arc_msg);
|
match (*sender_lock).get(&id).unwrap().send(msg).await {
|
||||||
match (*sender_lock).get(&id).unwrap().send(arc_msg_clone).await {
|
|
||||||
Ok(_) => println!("close message sent to thread no {}", id),
|
Ok(_) => println!("close message sent to thread no {}", id),
|
||||||
Err(e) => println!("My Error: {}", e)
|
Err(e) => println!("My Error: {}", e)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if arc_msg.is_text() || arc_msg.is_binary() {
|
else if msg.is_text() || msg.is_binary() {
|
||||||
let sender_lock = t_senders.lock().await;
|
let sender_lock = t_senders.lock().await;
|
||||||
for (iter_id, sender) in (*sender_lock).iter() {
|
for (iter_id, sender) in (*sender_lock).iter() {
|
||||||
let arc_msg_clone = Arc::clone(&arc_msg);
|
|
||||||
if *iter_id != id {
|
if *iter_id != id {
|
||||||
match (*sender).send(arc_msg_clone).await {
|
match (*sender).send(msg.clone()).await {
|
||||||
Ok(_) => println!("message sent to thread no {}", *iter_id),
|
Ok(_) => println!("message sent to thread no {}", *iter_id),
|
||||||
Err(e) => println!("My Error: {}", e)
|
Err(e) => println!("My Error: {}", e)
|
||||||
}
|
}
|
||||||
|
@ -73,10 +70,9 @@ async fn main() {
|
||||||
let id = connection_count;
|
let id = connection_count;
|
||||||
loop {
|
loop {
|
||||||
match receiver.recv().await {
|
match receiver.recv().await {
|
||||||
Some(arc_msg) => {
|
Some(msg) => {
|
||||||
println!("Message received by thread {}", id);
|
println!("Message received by thread {}", id);
|
||||||
println!("The message is {}", arc_msg);
|
println!("The message is {}", msg);
|
||||||
let msg = (*arc_msg).clone();
|
|
||||||
match websocket_send.send(msg).await {
|
match websocket_send.send(msg).await {
|
||||||
Ok(_) => println!("message sent to client {}", id),
|
Ok(_) => println!("message sent to client {}", id),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue