Skip to content

Instantly share code, notes, and snippets.

@NorioKobota
Created October 16, 2015 06:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NorioKobota/e001650970fb9ddbb472 to your computer and use it in GitHub Desktop.
Save NorioKobota/e001650970fb9ddbb472 to your computer and use it in GitHub Desktop.
exchange binary data between C++ and JavaScript by using linear-rpc - C++ part
/**
* $ git clone --recursive https://github.com/linaer-rpc/linear-cpp /path/to
* $ cd /path/to/linear-cpp
* $ ./bootstrap && ./configure && make
* $ cp /this/foo.cpp /path/to/linear-cpp/sample
* $ cd sample
* $ g++ -I ../include -I ../deps/msgpack/include -o foo foo.cpp ../src/.libs/liblinear.a ../deps/libtv/src/.libs/libtv.a ../deps/libtv/deps/libuv/.libs/libuv.a
* $ ./foo
*/
#include <iostream>
#include "linear/ws_server.h"
namespace sample {
class Handler : public linear::Handler {
public:
void OnMessage(const linear::Socket& socket, const linear::Message& message) {
switch(message.type) {
case linear::REQUEST:
{
linear::Request request = message.as<linear::Request>();
if (request.method == "getBinary") {
linear::type::binary bin("\0\1\2", 3); // ptr, size <= create this from pgsql data.
linear::Response response(request.msgid, bin);
response.Send(socket);
}
}
break;
default:
break;
}
}
};
} // namespace sample
int main() {
sample::Handler h;
linear::WSServer server(h);
server.Start("0.0.0.0", 37800);
std::cout << "press return to exit" << std::endl;
std::string p;
std::getline(std::cin, p);
server.Stop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment