astahjmo

astahjmo

I’m trying to use Gun to communicate with the Discord API for educational purposes (I chose Gun for being a lower-level tool to understand some complexities), and I’m having issues during the handshake with the Discord WebSocket gateway. I’m receiving the following error:

(MatchError) no match of right hand side value: {:error, {:down, {:shutdown, {:tls_alert, {:internal_error, ~c"TLS client: In state wait_cert_cr at ssl_handshake .erl:364 generated CLIENT ALERT: Fatal - Internal Error\n {unexpected_error,undef}"}}}}}

Please note that my :gun.ws_upgrade is failing during the handshake when transitioning from HTTP to WebSocket. I have no idea what might be wrong to reach this point with this error. Can you shed some light on what could be the issue and how to fix it?

Here is my code

  def test do
    opts2 = %{
        connect_timeout: 60000,
        retry: 10,
        retry_timeout: 300,
        transport: :tls,
        tls_opts: [
          verify: :verify_none,
          cacerts: :certifi.cacerts(),
          depth: 99,
          reuse_sessions: false
        ],
        http_opts: %{version: :"HTTP/1.1"},
        protocols: [:http],
    }
    opts = %{
        connect_timeout: 60000,
        retry: 10,
        retry_timeout: 300,
        transport: :tls,
        tls_opts: [
          verify: :verify_peer,
          cacertfile: CAStore.file_path(),
          depth: 99,
          reuse_sessions: false,
          verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'gateway.discord.gg']}
        ],
        http_opts: %{version: :"HTTP/1.1"},
        protocols: [:http],
    }
    {:ok, conn} = :gun.open('discord.com', 443, opts2)
    {:ok, _} = :gun.await_up(conn)
    stream = :gun.get(conn, "/api/v10/gateway")
    {response, :nofin, 200, headers} = :gun.await(conn, stream)
    {:ok, body} = :gun.await_body(conn, stream)
    {:ok, map} = Jason.decode(body)
    IO.inspect(map["url"])

    {:ok, conn} = :gun.open('gateway.discord.gg', 443, opts)
    {:ok, _} = :gun.await_up(conn)
    :timer.sleep(1000)
    stream = :gun.ws_upgrade(conn, '/?v=10&encoding=json', headers, opts)
    receive do
      {:gun_upgrade, _, _} ->
        IO.puts("WebSocket Upgrade Successful")
        connected(conn)
      {:gun_error, _reason} ->
        IO.puts("WebSocket Upgrade Error")
    after
      5000 ->
        IO.puts("WebSocket Upgrade Timeout")
    end
  end

  defp connected(conn) do
    receive do
      something ->
        IO.puts("Received WebSocket Text Frame")
        connected(conn)
      {:gun_close, _status, _reason} ->
        IO.puts("WebSocket Connection Closed")
    after
      5000 ->
        IO.puts("WebSocket Communication Timeout")
    end
  end

Showing Posts 1 to 1

7stud

7stud

I got a gun example with a websocket upgrade to work a long time ago. Maybe it will help.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews