websockets-rs / rust-websocket

A WebSocket (RFC6455) library written in Rust

Home Page:http://websockets-rs.github.io/rust-websocket/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Closing connection

colinbes opened this issue · comments

I am using crate to connect ws client to a device server using socket.io which is able to switch to websockets. I am connecting to device using url ws://ipaddress/socket.io/?EIO=3&transport=websocket.

This connects fine and I am getting messages from the device but after about 80 seconds it receives a Close notification which I am unable to track down. If I connect via browser to device and connect using socket.io library the connection stays alive. In browser code (using socket.io) I am not specifically sending and packets to the device so am not sure if I am getting Close message due to me not sending any data to the device (device with socket server) or whether Close is generated on websocket Client side due to too infrequent messages (server sends message roughly every 20 seconds).

Should I be sending periodic messages to the server? Or is it due to infrequent messages from server (I don't think it is this).

Appreciate any feedback ideas - busy digging into info....

You can debug unencrypted (ws://) WebSocket messages using network investigation tool like Wireshark. You should see whether there is a message with opcode: Connection close (8).

I know I am getting close message with status code of 1000. I don't know why I am getting it though.

As a quick hack, I added simple doTick function to periodically send short text message to server - now I don't get the close message and my connection stays connected.

Now looking for best way to do this - I don't see way to do this via websocket crate settings - am I missing something, can this be handled by websocket crate or should I implement this myself?

Battling a bit with my newness to Rust on best way to accomplish this.

	async fn do_tick(sender: &mut Writer<TcpStream>) {
		let mut interval = time::interval(Duration::from_secs(10));
		loop {
			interval.tick().await;
			println!("tick");
			let _ = sender.send_message(&OwnedMessage::Text(String::from("2"))).unwrap();
		}
	}
	
	let _ = do_tick(&mut sender).await;