libpomelo使用

客户端访问gate服务器,获得连接后再访问connect服务器

//pre define
#define GATE_HOST "127.0.0.1"
#define GATE_PORT 3014
#define MAX_LINE_CHARS 1024
#define MAX_RUN_NUM 5000000
#define END_STR "bye"
#define ROBOT_STR "robot"

static const char *connectorHost = "";
static int connectorPort = 0;
static const char *user = "";
static const char *channel = "";
static pc_client_t *pomelo_client;
void login(const char *username, const char *Channel) {
    const char *ip = GATE_HOST;
    int port = GATE_PORT;
    user = username;
    channel = Channel;
    //init a client
    pc_client_t *client = pc_client_new();

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

    // set the address
    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);
    printf("try to connect to gate server %s %d\n", ip, port); //debug 

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

    // try to route 
    const char *route = "gate.gateHandler.queryEntry";

    // make the json msg
    json_t *msg = json_object();
    json_t *str = json_string(username);
    json_object_set_new(msg, "uid", str);

    // make request
    pc_request_t *request = pc_request_new();
    pc_request(client, request, route, msg, on_request_gate_cb);
}

客户端收到gate返回的信息(connectorHost & connectorPort)on_request_gate_cb;进行重连接

void on_request_gate_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) {
        // parse the msg get
        connectorHost = json_string_value(json_object_get(resp, "host"));
        connectorPort = json_number_value(json_object_get(resp, "port"));

        //try to reconnect
        pc_client_t *client = pc_client_new_with_reconnect(1, 30, 1);

        struct sockaddr_in address;
        memset(&address, 0, sizeof(struct sockaddr_in));
        address.sin_family = AF_INET;
        address.sin_port = htons(connectorPort);
        address.sin_addr.s_addr = inet_addr(connectorHost);
        // add pomelo events listener
        pc_add_listener(client, PC_EVENT_TIMEOUT, on_timeout);
        pc_add_listener(client, "disconnect", on_disconnect);
        pc_add_listener(client, "onChat", on_chat);
        pc_add_listener(client, "onAdd", on_add);
        pc_add_listener(client, "onLeave", on_leave);
        pc_add_listener(client, "reconnect", on_reconnect);
        printf("try to connect to connector server %s %d\n", connectorHost, connectorPort);//debug
        // try to connect to server.
        if (pc_client_connect(client, &address)) {
            printf("fail to connect connector server.\n");
            pc_client_destroy(client);
            return ;
        }
        //try route
        const char *route = "connector.entryHandler.enter";
        //msg
        json_t *msg = json_object();
        json_t *str = json_string(user);
        json_t *channel_str = json_string(channel);
        json_object_set_new(msg, "username", str);
        json_object_set_new(msg, "rid", channel_str);
        //request
        pc_request_t *request = pc_request_new();
        printf("%s %s\n", user, channel);
        pc_request(client, request, route, msg, on_request_connector_cb);
    }
    // release relative resource with pc_request_t
    json_t *pc_msg = req->msg;
    pc_client_t *pc_client = req->client;
    json_decref(pc_msg);
    pc_request_destroy(req);
    pc_client_stop(pc_client);//stop wait for infomation
}

客户端连接到connector,connector返回用户信息

服务器端connectorHandler

module.exports = function(app) {
    return new Handler(app);
};

var Handler = function(app) {
        this.app = app;
};

var handler = Handler.prototype;

/**
 * New client entry chat server.
 *
 * @param  {Object}   msg     request message
 * @param  {Object}   session current session object
 * @param  {Function} next    next stemp callback
 * @return {Void}
 */
handler.enter = function(msg, session, next) {
    var self = this;
    var rid = msg.rid;
    var uid = msg.username + '*' + rid
    var sessionService = self.app.get('sessionService');

    //duplicate log in
    if( !! sessionService.getByUid(uid)) {
        next(null, {
            code: 500,
            error: true
        });
        return;
    }

    session.bind(uid);
    session.set('rid', rid);
    session.push('rid', function(err) {
        if(err) {
            console.error('set rid for session service failed! error is : %j', err.stack);
        }
    });
    session.on('closed', onUserLeave.bind(null, self.app));

    //put user into channel
    self.app.rpc.chat.chatRemote.add(session, uid, self.app.get('serverId'), rid, true, function(users){

        //here return all users information
        next(null, {
            users:users
        });
    });
};

摘要

        //put user into channel
        self.app.rpc.chat.chatRemote.add(session, uid, self.app.get('serverId'), rid, true, function(users){

        //here return all users information
        next(null, {
            users:users
        });

客户端响应函数on_request_connector_cb

void on_request_connector_cb(pc_request_t *req, int status, json_t *resp) {
    printf("on_request_connector_cb\n");
    if (status == -1) {
        printf("Fail to send request to server.\n");
    } else if (status == 0) {
        char *json_str = json_dumps(resp, 0);
        //print all users information
        printf("server response: %s \n", json_str);   
        //get msg 
        json_t *users = json_object_get(resp, "users");
        if (json_object_get(resp, "error") != NULL) {
            printf("connect error %s", json_str);
            free(json_str);
            return;
        }
        //set the finall client
        pomelo_client = req->client;
        printf("login chat ok\n");
    }

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

    //here not destroy the client
}

客户端发送消息

void msg_send(const char *message, const char *rid, const char *from, const char *target) {
    //route
    const char *route = "chat.chatHandler.send";

    //msg
    json_t *msg = json_object();
    json_t *str = json_string(message);
    json_object_set_new(msg, "content", str);
    json_object_set_new(msg, "rid", json_string(rid));
    json_object_set_new(msg, "from", json_string(from));
    json_object_set_new(msg, "target", json_string(target));

    //request
    pc_request_t *request = pc_request_new();
    pc_request(pomelo_client, request, route, msg, on_send_cb);
}


void on_send_cb(pc_request_t *req, int status, json_t *resp) {
    if(status == 0){
        printf("on_send_cb ok\n");
    } else {
        printf("on_send_cb bad\n");
    }

    // release relative resource with pc_request_t
    json_t *msg = req->msg;
    json_decref(msg);
    pc_request_destroy(req);
}

客户端接受消息(以on_chat为例)

void on_chat(pc_client_t *client, const char *event, void *data) {
    json_t *json = (json_t * )data;
    const char *msg = json_dumps(json, 0);
    printf("%s %s\n", event, msg);

    //here donot need to release, because the information is from the Server

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值