dayjaby / JSONServer

A simple server written in C++ able to handle JSON data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON-Server

The server is designed to be very easy to use, allowing you to write a server in a few lines of codes.

Motivating Example

The following example illustrates how to make the server read and write a message.
boost::asio::io_service io;
GenericServer server(io,12122);
server.onAccept(
[&server](std::shared_ptr<GenericSession> session) {
    /* Connection established */
    session->onRead([session](rapidjson::Document& d){
        /* JSON message read successfully */
        rapidjson::StringBuffer s;
        rapidjson::Writer<rapidjson::StringBuffer> writer(s);
        writer.StartObject();
        writer.String("Goodbye");
        writer.String(d["hello"].GetString());
        writer.EndObject();
        session->writeJson(s,[](){
          /* JSON message sent successfully */
        });
    });
    session->start();
});

Prerequisites

C++ Boost::Asio

Usage

On Debian-like systems, just go to the directory and enter: make

There are several clients shipped as well. For example to connect with and write to the server, you can use the following code.

<?php
  require("JsonClient.php");
  $session = new Session("127.0.0.1", 12122);
  echo $session->execute("{\"hello\":\"world\"}");
?>

If you are running the example server, you get {"Goodbye":"world"} as response.

About

A simple server written in C++ able to handle JSON data


Languages

Language:C++ 97.2%Language:Makefile 1.5%Language:PHP 1.3%