socketry / async-websocket

Asynchronous WebSocket client and server, supporting HTTP/1 and HTTP/2 for Ruby.
MIT License
174 stars 17 forks source link

After the computer wakes up from sleeping, my program is stuck without any errors #70

Open wuminzhe opened 4 months ago

wuminzhe commented 4 months ago
#!/usr/bin/env ruby

require 'async'
require 'async/http/endpoint'
require 'async/websocket/client'

URL = "wss://polkadot-rpc.dwellir.com"

Async do |task|
  endpoint = Async::HTTP::Endpoint.parse(URL)

  Async::WebSocket::Client.connect(endpoint) do |connection|
    connection.write(Protocol::WebSocket::TextMessage.generate({
      "jsonrpc" => "2.0", id: 1, method: "chain_subscribeNewHead", params: []
    }))

    while message = connection.read
      puts message.inspect
    end
  end
end

This is a very simple program that subscribes to new head and prints out it. I found that when my computer is woken up from sleeping, the program will stop working without any errors.

I'm quite new to this gem and async ruby. How do I handle this situation?

ioquatix commented 4 months ago

Interesting. Do you mind describing a bit more about your computer?

wuminzhe commented 4 months ago

Interesting. Do you mind describing a bit more about your computer?

I run the program in ubuntu which is running inside windows 11 wsl2.

I'll try it in macos.

ioquatix commented 4 months ago

It sounds like the connection could be disconnected, but the disconnection is not reported correctly.

wuminzhe commented 4 months ago

macos has no such problem. my macos version is 14.5

ioquatix commented 4 months ago

Thanks for your report. I'll check if I can reproduce it on native Linux.

wuminzhe commented 4 months ago

@ioquatix Thanks for the your great gems. They made it easy for me to make an elegant websocket json-rpc client. But I'm not quite sure if I'm using them correctly. Can you have a look?

The websocket client https://github.com/wuminzhe/scale_rb/blob/main/lib/client/ws_client.rb

examples: https://github.com/wuminzhe/scale_rb/blob/main/examples/ws_client_1.rb https://github.com/wuminzhe/scale_rb/blob/main/examples/ws_client_2.rb https://github.com/wuminzhe/scale_rb/blob/main/examples/ws_client_3.rb https://github.com/wuminzhe/scale_rb/blob/main/examples/ws_client_4.rb

ioquatix commented 4 months ago

Sure, I'll take a look.

I'm glad the libraries made it easy for you. It was one of my goals to make it easier to build such things with Ruby.