Hello,
I just finished the book "the rust programming language" and i am now working on a WebSocket server.
I am trying to make it impossible to send two messages in a row: It should receive a message and send one.
I created an object that looks like this.
pub struct Connection {
id : u128,
event_sender: Sender<Event>,
socket : Socket
}
The event_sender
sends back a notification to the main thread (either connection created, message received, or connection closed)
The socket is what makes me able to interact with the client, and here is my problem, all the implementation of connection are mut self
, so they always consume the connection.
And my idea was to send the connection inside the notification so when the connection comes into the main thread; the main thread can do
connection.send(message)
and the connection will be consumed and waiting for a new message to be received.
The big problem is here :
fn send_connection_opened_notification(mut self) {
self.event_sender.send(Event::ConnectionOpened(self).expect("Could not send a message back to the main thread");
}
pub async fn send_message(mut self, message: String){
self.socket.send(Message::Text(message)).await.expect("Message could not be sent");
self.handle_next_msg();
}
}
So I wanted the connection to be passed back and forth without the possibility of sending two messages in a row.
I am trying to move the connection itself through a channel.
I want the connection to be consumed, Is there any ways?
Do I have to think about the design again?
I found this solution to be elegant.
Thank you a lot