Socket (sockets v2.1.6)
Copy MarkdownThe top-level entry point for opening network connections.
Instead of picking a specific socket type upfront, you can pass a URI like
"tcp://example.com:80" or "wss://example.com/chat" and this module
figures out the right type for you. Supports TCP (tcp://), SSL (ssl://), UDP (udp://), WebSocket (ws://),
and secure WebSocket (wss://) — for both outbound connections (connect)
and inbound listeners (listen).
Summary
Functions
Create a socket connecting to somewhere using an URI.
Create a socket connecting to somewhere using an URI, raising if an error
occurs, see connect.
Create a socket listening somewhere using an URI.
Create a socket listening somewhere using an URI, raising if an error occurs,
see listen.
Types
@type t() :: Socket.Protocol.t()
Functions
Create a socket connecting to somewhere using an URI.
Supported URIs
tcp://host:portfor Socket.TCPssl://host:portfor Socket.SSLws://host:port/pathfor Socket.Web (using Socket.TCP)wss://host:port/pathfor Socket.Web (using Socket.SSL)udp://host:portfor Socket:UDP
Example
{ :ok, client } = Socket.connect "tcp://google.com:80"
client.send "GET / HTTP/1.1\r\n"
client.recv
Create a socket connecting to somewhere using an URI, raising if an error
occurs, see connect.
Create a socket listening somewhere using an URI.
Supported URIs
If host is * it will be converted to 0.0.0.0.
tcp://host:portfor Socket.TCPssl://host:portfor Socket.SSLws://host:port/pathfor Socket.Web (using Socket.TCP)wss://host:port/pathfor Socket.Web (using Socket.SSL)udp://host:portfor Socket:UDP
Example
{ :ok, server } = Socket.listen "tcp://*:1337"
client = server |> Socket.accept!(packet: :line)
client |> Socket.Stream.send(client.recv)
client |> Socket.Stream.close
Create a socket listening somewhere using an URI, raising if an error occurs,
see listen.