1.源码实现
#include <iostream>
#include <sim/sim.h>
using namespace std;
class MyHandler : public sim::Handler {
public:
virtual sim::HandlerState proc(const sim::Request &req, sim::Response *resp)
{
std::string cmd = req.msg.type();
if(cmd == "ping")
{
resp->msg.add("ok");
resp->msg.add("pong");
}
else
{
resp->msg.add("ok");
resp->msg.add("cmd");
}
return this->resp();
}
};
int main()
{
const char *ip = "localhost";
int port = 8080;
sim::Server *serv = sim::Server::listen(ip, port);
MyHandler handler;
serv->add_handler(&handler);
serv->loop();
return 0;
}
2.编译源码
$ g++ -o test test.cpp -std=c++11 -lsim
3.运行及其结果
$ ./test
$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
ping
0=ok 1=pong
pong
0=ok 1=cmd
^]
telnet> quit
Connection closed.
网友评论