Skip to content

Instantly share code, notes, and snippets.

@kyle-eshares
Created March 1, 2016 17:07
Show Gist options
  • Save kyle-eshares/f7073cfc77d6d362d0b8 to your computer and use it in GitHub Desktop.
Save kyle-eshares/f7073cfc77d6d362d0b8 to your computer and use it in GitHub Desktop.
defmodule SimpleTCP.Worker do
import Socket
def start_link(port) do
pid = spawn_link(fn -> init(port) end)
{:ok, pid}
end
def init(port) do
server = Socket.TCP.listen!(port, [packet: :line])
loop_connection(server)
end
defp loop_connection(server) do
# Accept a TCP connection
client = Socket.TCP.accept!(server)
# Start our sending process.
{:ok, _pid} = SimpleTCP.Sender.start_link(client)
# Start our listening process in another process so it doesn't block
spawn(fn() -> listen_for_msg(client) end)
# Get the next connection
loop_connection(server)
end
defp listen_for_msg(client) do
case Socket.Stream.recv(client) do
{ :ok, data } ->
# Use gproc to cast the message {:msg, data} to everyone subscribed to :something
GenServer.cast({:via, :gproc, {:p, :l, :something}}, {:msg, data})
# Loop for another message
listen_for_msg(client)
{ :error, :closed } -> :ok
other -> IO.inspect other
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment