[原]pomelo開發環境搭建


pomelo基於nodejs服務器開源框架,比較牛逼的!

1、安裝nodejs(官網下載地址) 安裝python等 具體見官網說明

2、安裝pomelo(見官方步驟)或者 http://blog.csdn.net/wangqiuyun/article/details/9243263

3、demo無法運行說明,1.1.1版創建出的demo瀏覽器點start server無響應,用firebug發現報“ReferenceError: Buffer is not defined” 這是bug解決辦法 我不會用 所以我不用瀏覽器測試,采用非瀏覽器測試 (不過這說明你的環境配置成功了)
4、自己編譯libpomelo庫

(1)下載gyp   命令行:git clone https://github.com/martine/gyp.git  沒有安裝git的童鞋 可以直接下zip

  進入gyp目錄 命令行 執行 setup.py install  安裝gyp

(2)下載libpomelo  命令行:git clone https://github.com/NetEase/libpomelo.git 或者 下載zip

  使用gyp創建 libpomelo工程。 cmd 到 gyp根目錄 執行 gyp.bat --depth=. libpomelo根路徑/pomelo.gyp -Dlibrary=static_library -DTO=pc 

成功之后就會在libpomelo下創建出vs工程

(3)編譯libpomelo靜態庫

  打開以上工程,選則整個解決方案,生成解決方案 就會編譯出靜態庫 jansson.lib libpomelo.lib libuv.lib

(4)如何使用libpomelo.lib

 創建C++ win32工程,引用靜態庫

A、添加工程的頭文件目錄:工程---屬性---配置屬性---c/c++---常規---附加包含目錄:加上頭文件存放目錄。
B、添加文件引用的lib靜態庫路徑:工程---屬性---配置屬性---鏈接器---常規---附加庫目錄:加上lib文件存放目錄。
C 然后添加工程引用的lib文件名:工程---屬性---配置屬性---鏈接器---輸入---附加依賴項:加上lib文件名。

說明:libpomelo沒有提完整的頭文件路徑 libpomelo\include只包含libpomelo自身的 jansson libuv 頭文件需要去\libpomelo\deps下查找

最終的include文件如圖:

(5)libuv.lib庫運行需要 ws2_32.lib IPHLPAPI.lib Psapi.lib 按照“然后添加工程引用的lib文件名:工程---屬性---配置屬性---鏈接器---輸入---附加依賴項:加上lib文件名” 添加幾個庫

(6)運行代碼測試:

#ifdef _WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#include "pomelo.h"

const char *ip = "127.0.0.1";
int port = 3010;

// 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);

  pc_client_stop(client);
}

void do_request(pc_client_t *client) {
  // compose request
  const char *route = "connector.entryHandler.entry"; //改成默認web-server的設置
  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);
}

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


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

  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);

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

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

  do_request(client);

  // main thread has nothing to do and wait until child thread return.
  pc_client_join(client);

  // release the client
  pc_client_destroy(client);

  return 0;
}

成功后的提示 :

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM