So I am trying to implement socket communication between a node server and rust client. I am however stuck. No matter what I try, I get the error:
Error: String( "EngineIO Error", ) thread '' panicked at 'EngineIO Error'
I do however receive the connected console.log in the node server, anyone have an idea?
Client code in rust
use rust_socketio::{Client, ClientBuilder, Payload};
use serde_json::json;
use std::time::Duration;
fn main() {
// define a callback which is called when a payload is received
// this callback gets the payload as well as an instance of the
// socket to communicate with the server
let callback = |payload: Payload, socket: Client| {
match payload {
Payload::String(str) => println!("Received: {}", str),
Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data),
}
socket
.emit("test", json!({"got ack": true}))
.expect("Server unreachable")
};
// get a socket that is connected to the admin namespace
let socket = ClientBuilder::new("http://localhost:4122")
.namespace("/admin")
.on("test", callback)
.on("error", |err, _| eprintln!("Error: {:#?}", err))
.connect()
.expect("Connection failed");
// emit to the "foo" event
let json_payload = json!({"token": 123});
socket
.emit("foo", json_payload)
.expect("Server unreachable");
// define a callback, that's executed when the ack got acked
let ack_callback = |message: Payload, _| {
println!("Yehaa! My ack got acked?");
println!("Ack data: {:#?}", message);
};
let json_payload = json!({"myAckData": 123});
// emit with an ack
socket
.emit_with_ack("test", json_payload, Duration::from_secs(2), ack_callback)
.expect("Server unreachable");
socket.disconnect().expect("Disconnect failed")
}
Server code node
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 4122;
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
io.of("/admin").on("connection", (socket) => {
console.log('connected')
socket.emit('test','jd')
socket.on("admin:foo", () => {
console.log()
socket.emit('test','jd')
console.log('test')
});
});
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`);
});
For socket.io ,you should know it is asynchronous, the event may be not occured before socket.disconnect() you can add loop simply for test like this
For Server code node, the event name for admin:foo should be foo, and the event for test was not added, you should change the code like this for test