I have the following code:
use actix_web::{web::{self, Data}, App, HttpServer, dev::Server};
use crate::{connection::wss, entity::generic_entity::{GameEntity}, native::arena::Arena};
use crate::config;
/// Starts the game.
pub async fn init()
{
let server = GameServer
{
entities: Vec::new(),
arena: &Arena::new()
};
let _ = tokio::task::spawn(run_server(Data::new(server)));
loop
{
server.tick();
tokio::time::sleep(std::time::Duration::from_millis(1000 / 25));
};
}
/// Runs the WebSocket server.
async fn run_server(game_server: Data<GameServer<'static>>) -> Server
{
HttpServer::new(move || App::new()
.app_data(game_server).service(web::resource("/").to(wss::ws_on_connect)))
.bind(format!("127.0.0.1:{}", config::PORT))
.expect(format!("Cannot bind to port {}.", config::PORT).as_str())
.run()
}
/// A class which represents the entire game server.
pub struct GameServer<'a>
{
/// All of the entities in the arena.
pub entities: Vec<Option<GameEntity<'a>>>,
/// The arena representing the GameServer.
pub arena: &'a Arena
}
impl<'a> GameServer<'a>
{
pub fn tick(&mut self)
{
let mut shape_count: usize = 0;
for entity in self.entities.iter_mut()
{
match entity
{
Some(GameEntity::Tank(tank)) =>
{
tank.tick();
},
_ => ()
}
}
}
}
I have a WebSocket server which handles connections asynchronously to the game server loop. I want the HTTP server to store the game server as data so incoming connections can be handled by the game server accordingly. However, when I try passing the Data<GameServer> to the run_server function, I run into the following error:
error[E0277]: `(dyn ActorFuture<WebSocketClientHandler, Output = ()> + 'static)` cannot be shared between threads safely
How can I fix this?