English 中文(简体)
Elixir app exits instead of running forever
原标题:
  • 时间:2023-08-23 02:56:23
  •  标签:
  • elixir

I have coded simple elixir app, which starts websocket client and listens for incoming messages. I was expecting it to run forever. But the app exits as soon as I run it using mix app.start. Here is the code. What should I change to make it app run forever?

defmodule Example.WsClient do
  use WebSockex

  def start_link(state) do
    IO.puts "Starting WS Client"
    WebSockex.start_link("ws://example.com/ws", __MODULE__, state)
  end

  def handle_frame({type, msg}, state) do
    IO.puts "Received Message - Type: #{inspect type} -- Message: #{inspect msg}"
    {:ok, state}
  end

  def handle_cast({:send, {type, msg} = frame}, state) do
    IO.puts "Sending #{type} frame with payload: #{msg}"
    {:reply, frame, state}
  end
end
defmodule Example.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      # Starts a worker by calling: Example.Worker.start_link(arg)
      {Example.WsClient, []}
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Example.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
问题回答

暂无回答




相关问题
Elixir app exits instead of running forever

I have coded simple elixir app, which starts websocket client and listens for incoming messages. I was expecting it to run forever. But the app exits as soon as I run it using mix app.start. Here is ...

热门标签