Giter Site home page Giter Site logo

engine's Introduction

Engine

Swift Build Status CircleCI Code Coverage Codebeat Slack Status

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.

๐Ÿง Linux Ready

Deploy

๐Ÿ“ฆ Examples

Check out the HTTP, SMTP, and WebSockets examples in Sources/. You can clone this repository and run them on your computer.

๐Ÿ“˜ Overview

HTTP.Client

import HTTP

let response = try Client<TCPClientStream>.get("http://pokeapi.co/api/v2/pokemon/")
print(response)

HTTP.Server

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 http://localhost:\(port)/")
try server.start(responder: MyResponder()) { error in
    print("Got error: \(error)")
}

WebSocket Client

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")
    }
}

WebSocket Server

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 http://localhost:\(port)/")
try server.start(responder: MyResponder()) { error in
    print("Got server error: \(error)")
}

SMTP

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

๐Ÿ› Architecture

HTTP.Server

The HTTPServer is responsible for listening and accepting remote connections, then relaying requests and responses between the received connection and the responder.

HTTP.Client

The HTTPClient is responsible for establishing remote connections and relaying requests and responses between the remote connection and the caller.

๐Ÿ“– Documentation

Visit the Vapor web framework's documentation for instructions on how to use this package.

๐Ÿ’ง Community

Join the welcoming community of fellow Vapor developers in slack.

๐Ÿ”ง Compatibility

This package has been tested on macOS and Ubuntu.

engine's People

Contributors

artkay avatar bygri avatar czechboy0 avatar damuellen avatar edjiang avatar evertt avatar finestructure avatar harlanhaskins avatar jkubicek avatar joannis avatar ketzusaka avatar kylebshr avatar loganwright avatar matteocrippa avatar nathanflurry avatar obbut avatar p4t5h3 avatar piamancini avatar prayagverma avatar rosslebeau avatar rothomp3 avatar rugaard avatar sarbogast avatar shnhrrsn avatar steffendsommer avatar svanimpe avatar tanner0101 avatar timominous avatar tnantoka avatar tqtifnypmb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.