Engine is a collection of low level transport protocols implemented in pure Swift intended for use in server side and client side applications. It is used as the core transport layer in Vapor.
Check out the HTTP, SMTP, and WebSockets examples in Sources/
. You can clone this repository and run them on your computer.
import HTTP
let response = try Client<TCPClientStream>.get("https://pokeapi.co/api/v2/pokemon/")
print(response)
import HTTP
final class MyResponder: Responder {
func respond(to request: Request) throws -> Response {
let body = "Hello World".makeBody()
return Response(body: body)
}
}
let server = try Server<TCPServerStream, Parser<Request>, Serializer<Response>>(port: port)
print("visit https://localhost:\(port)/")
try server.start(responder: MyResponder()) { error in
print("Got error: \(error)")
}
import Engine
import WebSockets
try WebSocket.connect(to: url) { ws in
print("Connected to \(url)")
ws.onText = { ws, text in
print("[event] - \(text)")
}
ws.onClose = { ws, _, _, _ in
print("\n[CLOSED]\n")
}
}
import HTTP
import WebSockets
final class MyResponder: Responder {
func respond(to request: Request) throws -> Response {
return try request.upgradeToWebSocket { ws in
print("[ws connected]")
ws.onText = { ws, text in
print("[ws text] \(text)")
try ws.send("π \(text)")
}
ws.onClose = { _, code, reason, clean in
print("[ws close] \(clean ? "clean" : "dirty") \(code?.description ?? "") \(reason ?? "")")
}
}
}
}
let server = try Server<TCPServerStream, Parser<Request>, Serializer<Response>>(port: port)
print("Connect websocket to https://localhost:\(port)/")
try server.start(responder: MyResponder()) { error in
print("Got server error: \(error)")
}
import SMTP
let credentials = SMTPCredentials(
user: "server-admin-login",
pass: "secret-server-password"
)
let from = EmailAddress(name: "Password Reset",
address: "[email protected]")
let to = "[email protected]"
let email: Email = Email(from: from,
to: to,
subject: "Vapor SMTP - Simple",
body: "Hello from Vapor SMTP π")
let client = try SMTPClient<TCPClientStream>.makeGmailClient()
try client.send(email, using: credentials)
Emails using the Gmail client are blocked by default unless you enable access for less-secure apps in your Gmail account settings: https://support.google.com/accounts/answer/6010255?hl=en-GB
The HTTPServer is responsible for listening and accepting remote connections, then relaying requests and responses between the received connection and the responder.
The HTTPClient is responsible for establishing remote connections and relaying requests and responses between the remote connection and the caller.
Visit the Vapor web framework's documentation for instructions on how to use this package.
Join the welcoming community of fellow Vapor developers in slack.
This package has been tested on macOS and Ubuntu.