This commit is contained in:
2026-02-15 13:55:39 +08:00
parent 7811a9ad48
commit a116598698
5 changed files with 97 additions and 8 deletions

26
lib/broker.ex Normal file
View File

@@ -0,0 +1,26 @@
defmodule Broker do
use GenServer
def init(opts) do
{:ok, listen_socket} =
:gen_tcp.listen(opts[:port], [
:binary,
reuseaddr: true,
active: false,
backlog: 100
])
{:ok, opts}
end
defp accept_loop(listen_socket) do
case :gen_tcp.accept(listen_socket) do
{:ok, socket} ->
spawn(__MODULE__, :handle_client, [socket])
accept_loop(listen_socket)
{:error, reason} ->
IO.puts("接受連接失敗: #{reason}")
end
end
end