rerun-io / ewebsock

A Rust Websocket client that compiles to both native and web
Apache License 2.0
219 stars 28 forks source link

WebSocket Server Reports Unexpected EOF on Connect, but ewebsock Returns Ok #5

Closed danforbes closed 1 year ago

danforbes commented 1 year ago

I'm trying to connect to the WebSocket server exposed by a Polkadot node. Here is my code:

#[tokio::main]
async fn main() {
    let mut ws = match ewebsock::connect("ws://100.115.92.194:9944") {
        Ok(ws) => ws,
        _ => panic!("Cannot connect to WebSocket server"),
    };

    ws.0.send(WsMessage::Text(
        "{ \"id\": \"metadata\", \"method\": \"state_getMetadata\", \"jsonrpc\": \"2.0\" }"
            .into(),
    ));

    while let Some(event) = ws.1.try_recv() {
        println!("{:?}", event);
    }
}

The program terminates without exception, but does not print anything either. In the logs of the Polkadot node, I see the following message:

Rejected connection: Transport(i/o error: unexpected end of file

Caused by:
    unexpected end of file)

Is this due to user error?

danforbes commented 1 year ago

Looks like other people are seeing this error when connecting to Polkadot nodes as well. I am only seeing the problem when I try to connect from this library (the connection works fine from a JavaScript WebSocket instance).

emilk commented 1 year ago

By using try_recv, you will stop looping as soon as there is no message (which is likely right away). This means you will never actually get back an answer, and the connection will drop immediately. This could be part of the answer.

Is this on native or web btw?

danforbes commented 1 year ago

...you will stop looping as soon as there is no message (which is likely right away).

Yes, I thought this may be the problem, but I didn't see any examples or anything that demonstrated other ways to explore the capabilities of this library.

I am running this natively.

emilk commented 1 year ago

I didn't see any examples

https://github.com/rerun-io/ewebsock/blob/main/example_app/src/app.rs