jsoneaday
jsoneaday11mo ago

fastwebsockets how to end client calls and avoid Error in websocket connection Unexpected EOF

What is the issue in this client code that causes the error?
pub async fn connect() -> Result<FragmentCollector<Upgraded>> {
let stream = TcpStream::connect("127.0.0.1:9001").await?;

let req = Request::builder()
.method("GET")
.uri("http://127.0.0.1:9001")
.header("Host", "localhost:9001")
.header(UPGRADE, "websocket")
.header(CONNECTION, "upgrade")
.header(
"Sec-Websocket-Key",
fastwebsockets::handshake::generate_key()
)
.header("Sec-Websocket-Version", "13")
.body(Body::empty())?;

let ws_result = handshake::client(&SpawnExecutor, req, stream).await;
match ws_result {
Ok((ws, _)) => {
Ok(FragmentCollector::new(ws))
},
Err(err) => {
Err(err.into())
}
}
}
async fn main() {
let mut ws = connect().await.unwrap();

let message = String::from("hello world");
let binding = message.clone();
let message = binding.as_bytes();

_ = ws.write_frame(Frame::text(Payload::from(message))).await;

_ = ws.write_frame(Frame::close(1000, &[]));
}
pub async fn connect() -> Result<FragmentCollector<Upgraded>> {
let stream = TcpStream::connect("127.0.0.1:9001").await?;

let req = Request::builder()
.method("GET")
.uri("http://127.0.0.1:9001")
.header("Host", "localhost:9001")
.header(UPGRADE, "websocket")
.header(CONNECTION, "upgrade")
.header(
"Sec-Websocket-Key",
fastwebsockets::handshake::generate_key()
)
.header("Sec-Websocket-Version", "13")
.body(Body::empty())?;

let ws_result = handshake::client(&SpawnExecutor, req, stream).await;
match ws_result {
Ok((ws, _)) => {
Ok(FragmentCollector::new(ws))
},
Err(err) => {
Err(err.into())
}
}
}
async fn main() {
let mut ws = connect().await.unwrap();

let message = String::from("hello world");
let binding = message.clone();
let message = binding.as_bytes();

_ = ws.write_frame(Frame::text(Payload::from(message))).await;

_ = ws.write_frame(Frame::close(1000, &[]));
}
0 Replies
No replies yetBe the first to reply to this messageJoin