GithubHelp home page GithubHelp logo

rwg_websocket's Introduction

RWG WebSocket

该框架实现简易HTTP协议及WebSocket协议。实现方式由epoll实现I/O多路复用。 服务将开启n个线程(包括主线程), n = _SC_NPROCESSORS_CONF + 1。 主线程将用于接收客户端请求,并分配到子线程中。子线程将处理传入数据、中断等事件。

该项目作为gitter_kid子项目。

实现简易HTTP服务器

#include "web/server.hpp"
#include <string>

void http_handler(rwg_web::req&, rwg_web::res& res, std::function<void ()>) {
    res.version() = "HTTP/1.1";
    res.status_code() = 200;
    res.description() = "OK";

    std::string content = "<!DOCTYPE html>              \
                           <html>                       \
                           <head>                       \
                           <title>Test Page</title>     \
                           </head>                      \
                           <body>                       \
                           <h1>Hello World</h1>         \
                           <p>This is a test page.</p>  \
                           </body>                      \
                           </html>";

    res.header_parameters()["Content-Type"] = "plain/html";
    res.header_parameters()["Content-Length"] = std::to_string(content.size());
    res.header_parameters()["Connection"] = "keep-alive";

    res.write_header();
    res.write(content.begin(), content.end());
    res.flush();
}

int main() {
    rwg_web::server server;
    server.listen("127.0.0.1", 8088);
    server.http_handle(http_handler);
    server.start();

    return 0;
}

rwg_websocket's People

Contributors

gscienty avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

linecode

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.