Networking
WebSockets, TCP, UDP and TLS are ordinary Otter statements. Connections are named values, and things that happen to them are events you handle with on.
Availability: included in the current release candidate (1.0.0). Whether it is part of the supported Otter 1.0 surface has not been decided yet, so details may change.
WebSockets
A WebSocket delivers whole messages. It works in console programs and in web applications.
Otter
connect to websocket "wss://echo.example.com" and call it chat
on open of chat
send "Hello" through chat
.
on message from chat
say "Got:" received message
close websocket chat
.
on error of chat
say "Problem:" websocket error
.
chat is connecting, open, closing, or closed - check it with chat is open, or read state of chat.
TCP
TCP is a stream of bytes, not messages, so its event is on data from and what arrives is always bytes. Otter never converts between text and bytes silently: turn text into bytes yourself before sending it.
Otter
connect to tcp "localhost" on port 9000 and call it connection
on connect of connection
message is bytes from text "Hello from Otter"
send message through connection
.
on data from connection
data is received data
say text from bytes data
close tcp connection
.
on error of connection
say network error
.
on close of connection
say "Disconnected"
.
Ask a connection about itself with state of connection (connecting, connected, closed), remote address of connection and remote port of connection. A connection that cannot be made goes to on error, then on close - or, with no on error handler, stops the program with a readable message.
TCP servers (listen for tcp) are reserved grammar and are not available yet. TCP clients are.
TLS
Add securely to make the same connection over TLS. The system checks the certificate chain, expiry, revocation, and that the certificate matches the server name. There is no way to turn that off: an untrusted certificate simply fails the connection.
Otter
connect securely to tcp "example.com" on port 443 and call it connection
on connect of connection
if connection is secure
say "secure"
.
say tls version of connection
.
for server "name" checks the certificate against a different name than the address you connect to. On this runtime (Windows PowerShell 5.1) protocol negotiation is not available, so using protocol stops with a clear message rather than connecting without it.
UDP
UDP sends independent datagrams. Open a socket, listen for data, and send to an address and port.
Otter
open udp on port 9000 and call it socket
on data from socket
say "Received from" sender address
response is bytes from text "Hello"
send response through socket
to sender address
on port sender port
.
open udp and call it socket picks a free port for you. close udp socket closes it.
Where these run
TCP, UDP and TLS are for console and desktop programs. A browser cannot open raw sockets, so the web target reports them as unsupported instead of imitating them. WebSockets work on both.
Next: cryptography and secrets