Skip to content

Commit 002007b

Browse files
authored
fix: hardcoded WebSocket port 3001 (#71)
1 parent 2406235 commit 002007b

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
6565
- [ ] Finish API Handler & add languages support
6666
- [ ] dev mode
67-
- [ ] websocket port now is hardcoded to `3001`
67+
- [x] websocket port now is hardcoded to `3001`
6868
- [ ] Granular Rebuilds
6969
- right now, the `rebuild_page()` function rebuilds both client and server bundles entirely, this is slow. only rebuild changed pages, and eventually, changed components
7070
- [ ] implement real HMR, not just restarting the page (line 34 in live_reload.js)

crates/metassr-server/src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl std::fmt::Display for RunningType {
5757

5858
pub struct ServerConfigs {
5959
pub port: u16,
60+
pub ws_port: u16,
6061
pub _enable_http_logging: bool,
6162
pub root_path: PathBuf,
6263
pub running_type: RunningType,
@@ -90,25 +91,31 @@ impl Server {
9091

9192
if let ServerMode::Development = self.configs.mode {
9293
info!("Configuring server for development mode");
93-
let live_reload_script = include_str!("scripts/live-reload.js");
94+
let ws_port = self.configs.ws_port;
95+
// Inject the ws_port into the live-reload script at runtime
96+
let live_reload_script =
97+
include_str!("scripts/live-reload.js").replace("__WS_PORT__", &ws_port.to_string());
9498
base_router = base_router.route(
9599
"/livereload/script.js",
96-
get(|| async {
97-
info!("Serving live-reload.js");
98-
axum::response::Response::builder()
99-
.header("Content-Type", "application/javascript")
100-
.body(live_reload_script.to_string())
101-
.unwrap()
100+
get(move || {
101+
let script = live_reload_script.clone();
102+
async move {
103+
info!("Serving live-reload.js");
104+
axum::response::Response::builder()
105+
.header("Content-Type", "application/javascript")
106+
.body(script)
107+
.unwrap()
108+
}
102109
}),
103110
);
104111
// Apply live reload middleware
105112
base_router = base_router.layer(axum::middleware::from_fn(inject_live_reload_script));
106113

107114
// Start the WebSocket server for live reload
108-
let ws_listener = TcpListener::bind("127.0.0.1:3001")
115+
let ws_listener = TcpListener::bind(format!("127.0.0.1:{ws_port}"))
109116
.await
110117
.map_err(|e| anyhow::anyhow!("WebSocket bind error: {}", e))?;
111-
debug!(
118+
info!(
112119
"WebSocket server listening on {:?}",
113120
ws_listener.local_addr()?
114121
);

crates/metassr-server/src/scripts/live-reload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
function connect() {
1212
if (ws) ws.close(); // Close old connection
13-
ws = new WebSocket('ws://localhost:3001')
13+
ws = new WebSocket('ws://localhost:__WS_PORT__')
1414
ws.onmessage = (event) => {
1515
const update = JSON.parse(event.data)
1616
const currentPath = window.location.pathname; //current page path

metassr-cli/src/cli/dev.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use super::traits::AsyncExec;
1818

1919
pub struct Dev {
2020
port: u16,
21+
ws_port: u16,
2122
// todo change this to a normal option, and edit impl asyncexec
2223
watcher: Arc<Mutex<Option<FileWatcher>>>,
2324
rebuilder: Arc<Rebuilder>,
@@ -26,14 +27,20 @@ pub struct Dev {
2627
}
2728

2829
impl Dev {
29-
pub fn new(port: u16, root_path: PathBuf, building_type: BuildingType) -> Result<Self> {
30+
pub fn new(
31+
port: u16,
32+
ws_port: u16,
33+
root_path: PathBuf,
34+
building_type: BuildingType,
35+
) -> Result<Self> {
3036
let (rebuild_tx, _) = broadcast::channel(100); //channel for rebuild notifications
3137

3238
let watcher = Arc::new(Mutex::new(None)); //FileWatcher::new()?;
3339
let rebuilder = Arc::new(Rebuilder::new(root_path.clone(), building_type)?);
3440

3541
Ok(Self {
3642
port,
43+
ws_port,
3744
watcher,
3845
rebuilder,
3946
root_path,
@@ -107,6 +114,7 @@ impl Dev {
107114

108115
let server_configs = ServerConfigs {
109116
port: self.port,
117+
ws_port: self.ws_port,
110118
_enable_http_logging: true,
111119
root_path: self.root_path.clone(),
112120
running_type: RunningType::ServerSideRendering,

metassr-cli/src/cli/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ pub enum Commands {
9090
},
9191

9292
Dev {
93+
/// port number on which the HTTP server will run
9394
#[arg(long, default_value_t = 8080)]
9495
port: u16,
96+
97+
/// port number for the WebSocket live reload server
98+
#[arg(long, default_value_t = 3001)]
99+
ws_port: u16,
95100
},
96101
}

metassr-cli/src/cli/runner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl AsyncExec for Runner {
3131

3232
let server_configs = ServerConfigs {
3333
port: self.port,
34+
ws_port: 0, // not used in production mode
3435
_enable_http_logging: self.allow_http_debug,
3536
root_path: current_dir()?,
3637
running_type,

metassr-cli/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ async fn main() -> Result<()> {
7474
} => {
7575
cli::Creator::new(project_name, version, description, template)?.exec()?;
7676
}
77-
Commands::Dev { port } => {
77+
Commands::Dev { port, ws_port } => {
7878
cli::Dev::new(
7979
port,
80+
ws_port,
8081
current_dir()?,
8182
metassr_build::server::BuildingType::ServerSideRendering,
8283
)?

0 commit comments

Comments
 (0)