I have a ClientManager
object, which manages join/leave actions of websocket clients (using simple_websockets
library), by fetching events from the lib's event_hub
. I create it in main()
:
1: let event_hub = simple_websockets::launch(8080)
2: .expect("failed to listen on port 8080");
3: let client_manager = ClientManager::new(event_hub);
The endless loop, which is processing events, is implemented in ClientManager::run()
method, so I launch it in a separate thread:
4: thread::spawn(|| client_manager.run() );
It handles the attaching and detaching clients, works as excepted. The problem comes when I want to use the client_manager
for other tasks, say, send a message to each attached clients:
5: client_manager.broadcast(String::from("hello"));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
I understand, that the ownership of client_manager
is transferred to the closure, so I couldn't use it anymore, but in this case, I'm not happy with this situation. client_manager
is running, I want to sent requests to it, but I already lost it at thread creation.
Can I start a thread without closure?
Probably, my whole conception is wrong, and I should not use threads for this task.