This is my main.rs file:
use axum::routing::get;
use log::info;
use serde_json::Value;
use socketioxide::{
extract::{AckSender, Bin, Data, SocketRef},
SocketIo,
};
use tracing_subscriber::FmtSubscriber;
fn on_connect(socket: SocketRef, Data(data): Data<Value>) {
info!("Socket.IO connected: {:?} {:?}", socket.ns(), socket.id);
socket.emit("auth", data).ok();
socket.on(
"message",
|socket: SocketRef, Data::<Value>(data), Bin(bin)| {
info!("Received event: {:?} {:?}", data, bin);
socket.bin(bin).emit("message-back", data).ok();
},
);
socket.on(
"message-with-ack",
|Data::<Value>(data), ack: AckSender, Bin(bin)| {
info!("Received event: {:?} {:?}", data, bin);
ack.bin(bin).send(data).ok();
},
);
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::subscriber::set_global_default(FmtSubscriber::default())?;
let (layer, io) = SocketIo::new_layer();
io.ns("/", on_connect);
io.ns("/custom", on_connect);
let app = axum::Router::new()
.route("/", get(|| async { "Hello, World!" }))
.layer(layer);
info!("Starting server");
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
Ok(())
}
and this is my Cargo.toml:
[dependencies]
tokio = { version = "1.36.0", features = ["full"] }
axum = "0.7.4"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.108"
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
diesel = "2.1.4"
diesel_logger = "0.3.0"
diesel_cli = "2.1.0"
socketioxide = "0.12"
log = "^0"
tracing = "0.1.37"
Problem is that when I run server, only HTTP part works, I get Hello, World!
from path /
, but when I try to connect to it using some SocketIO client on path /
or /custom
, I get only “connecting” and doesn’t connect, how could I fix it? My goal is to run both HTTP server with some API endpoints and SocketIO server for real time communication.