我编写了简单的 Elixir 应用程序,它启动 websocket 客户端并侦听传入的消息。我原以为它会永远运行。但是当我使用
mix app.start
运行应用程序时,该应用程序就会退出。这是代码。我应该更改什么才能使其应用程序永远运行?
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