Pomelo 的 C 语言客户端 libpomelo

码农软件 · 软件分类 · 游戏开发包 · 2019-10-05 09:56:43

软件介绍

libpomelo 是 Pomelo 的 C 语言客户端 SDK。

依赖

使用

创建客户端实例

// create a client instance.
pc_client_t *client = pc_client_new();

添加 listeners

// add some event callback.
  pc_add_listener(client, "onHey", on_hey);
  pc_add_listener(client, PC_EVENT_DISCONNECT, on_close);

Listener 定义

// disconnect event callback.
void on_close(pc_client_t *client, const char *event, void *data) {
  printf("client closed: %d.\n", client->state);
}

Pomelo defined listeners

#define PC_EVENT_DISCONNECT "disconnect"
#define PC_EVENT_TIMEOUT "timeout"
#define PC_EVENT_KICK "onKick"

连接服务器

 struct sockaddr_in address;

  memset(&address, 0, sizeof(struct sockaddr_in));
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr.s_addr = inet_addr(ip);

  // try to connect to server.
  if(pc_client_connect(client, &address)) {
    printf("fail to connect server.\n");
    pc_client_destroy(client);
    return 1;
  }

Send a notify

// notified callback
void on_notified(pc_notify_t *req, int status) {
  if(status == -1) {
    printf("Fail to send notify to server.\n");
  } else {
    printf("Notify finished.\n");
  }
  // release resources
  json_t *msg = req->msg;
  json_decref(msg);
  pc_notify_destroy(req);
}
// send a notify
void do_notify(pc_client_t *client) {
  // compose notify.
  const char *route = "connector.helloHandler.hello";
  json_t *msg = json_object();
  json_t *json_str = json_string("hello");
  json_object_set(msg, "msg", json_str);
  // decref json string
  json_decref(json_str);
  pc_notify_t *notify = pc_notify_new();
  pc_notify(client, notify, route, msg, on_notified);
}

Send a request

// request callback
void on_request_cb(pc_request_t *req, int status, json_t *resp) {
  if(status == -1) {
    printf("Fail to send request to server.\n");
  } else if(status == 0) {
    char *json_str = json_dumps(resp, 0);
    if(json_str != NULL) {
      printf("server response: %s\n", json_str);
      free(json_str);
    }
  }
  // release relative resource with pc_request_t
  json_t *msg = req->msg;
  pc_client_t *client = req->client;
  json_decref(msg);
  pc_request_destroy(req);
  // stop client
  pc_client_stop(client);
}
// send a request
void do_request(pc_client_t *client) {
  // compose request
  const char *route = "connector.helloHandler.hi";
  json_t *msg = json_object();
  json_t *str = json_string("hi~");
  json_object_set(msg, "msg", str);
  // decref for json object
  json_decref(str);
  pc_request_t *request = pc_request_new();
  pc_request(client, request, route, msg, on_request_cb);
}


本文地址:https://codercto.com/soft/d/16076.html

新媒体营销概论

新媒体营销概论

秋叶、刘勇 / 人民邮电出版社 / 2016-12-1 / 36.00

本书共分6章。第1章重点介绍了新媒体的概念和特征,引导读者全面认识新媒体所处的行业;第2章用历史发展的眼光,介绍了不同类型的新媒体,让读者不仅能看到最新的新媒体模式,也能看到这个模式发展背后的脉络;第3章重点介绍了新媒体广告投放载体,便于读者选择适合自己的新媒体运营方式;第4章介绍了新媒体运营的策划思维;第5章介绍了新媒体舆情管理知识;第6章选取了可口可乐、海底捞、恒大冰泉等的新媒体助力传统行业转......一起来看看 《新媒体营销概论》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具