libpomelo使用

2023-10-05 14:42
文章标签 使用 libpomelo

本文主要是介绍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 clientpc_client_t *client = pc_client_new();// add some event callback.pc_add_listener(client, PC_EVENT_DISCONNECT, on_gate_close);// set the addressstruct 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 msgjson_t *msg = json_object();json_t *str = json_string(username);json_object_set_new(msg, "uid", str);// make requestpc_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 getconnectorHost = json_string_value(json_object_get(resp, "host"));connectorPort = json_number_value(json_object_get(resp, "port"));//try to reconnectpc_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 listenerpc_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 routeconst char *route = "connector.entryHandler.enter";//msgjson_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);//requestpc_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_tjson_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 + '*' + ridvar sessionService = self.app.get('sessionService');//duplicate log inif( !! 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 channelself.app.rpc.chat.chatRemote.add(session, uid, self.app.get('serverId'), rid, true, function(users){//here return all users informationnext(null, {users:users});});
};

摘要

        //put user into channelself.app.rpc.chat.chatRemote.add(session, uid, self.app.get('serverId'), rid, true, function(users){//here return all users informationnext(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 informationprintf("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 clientpomelo_client = req->client;printf("login chat ok\n");}// release relative resource with pc_request_tjson_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) {//routeconst char *route = "chat.chatHandler.send";//msgjson_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));//requestpc_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_tjson_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}

这篇关于libpomelo使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/151414

相关文章

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

使用Python的requests库调用API接口的详细步骤

《使用Python的requests库调用API接口的详细步骤》使用Python的requests库调用API接口是开发中最常用的方式之一,它简化了HTTP请求的处理流程,以下是详细步骤和实战示例,涵... 目录一、准备工作:安装 requests 库二、基本调用流程(以 RESTful API 为例)1.

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3