跳到主要内容

request_client

简介

基于本pkg,可以快速开发对接各类公共服务器的pkg,例如pkg.social_client,pkg.auth_client等等。

信息

/pkg/request_client.gs一般作为组件使用,继承该组件用于创建连接各类公共服务器的客户端对象,例如/pkg/social_client/SocialClient.gs/pkg/auth_client/AuthClient.gs等。

构造函数para参数字段的说明

字段说明是否必有
host或者ip主机地址Y
port主机端口Y
using_websocket是否使用websocket协议N;缺省值为false
port_recv_size网络接收缓冲区大小,使用gs协议时起效N;缺省值为0
max_send_size网络允许发送的数据包大小,使用gs协议时起效N;缺省值为0
max_pending_count请求队列允许积压的阈值,超过该值时发送请求功能将暂时变得不可能直至恢复N;缺省值为0
keep_alive心跳间隔N;缺省值为60
auth_connection_callback连接主机成功后执行的登录主机验证的回调函数N;一般是在回调函数里发送验证消息
cookieless_msg_func本连接接收主机发送的无cookie消息的处理函数N;缺省值为nil,丢弃主机发送的无cookie消息
cookieless_msg_queue本连接接收主机发送的无cookie消息的队列N;缺省值为nil,丢弃主机发送的无cookie消息
proxy_connection_callback用于生成代理连接的生成回调N;缺省值为nil,不使用代理连接
request_timeout_seconds默认请求超时时间N;缺省值为30

组件接口

request_client.gs

函数原型函数作用
void enable_debug(bool enable)开启/关闭调试信息(显示收发消息)
bool send(string cmd, map args = )向目标主机发送指令(进队列),不等待指令结果
mixed send_request(string cmd, map args = , int timeout_seconds = -1)向目标主机发送指令(进队列),并且获取指令结果
bool send_priority(string cmd, map args = )优先向目标主机发送指令(进队列),不等待指令结果
mixed send_request_priority(string cmd, map args = , int timeout_seconds = -1)优先向主机发送指令(不进队列),并且获取指令结果
void set_auth_connection_callback(function fn)设置连接建立后验证登录的会调函数
bool is_connected()是否已经连接主机(包括已经连接并且通过身份认证)

样例

// SocialClient.gs
// created by linlc may/20/2021
//

#pragma parallel

import gs.lang.*;
import gs.util.*;

component pkg.request_client;

// 设置
readonly map _config := {};

void create(map para = nil)
{
if (is_static_object())
return;

_config := para;
assert(_config["account"], "Missing field in para: account");
assert(_config["server_id"], "Missing field in para: server_id");
assert(_config["auth_account_para"], "Missing field in para: auth_account_para");

set_auth_connection_callback((: auth_connection_callback :));
}

// 验证自己
public bool auth_account(map auth_args)
{
map args = {
"account" : _config["account"],
"server_id" : _config["server_id"],
} << auth_args;
mixed auth_result = send_request("cmd_auth_account", args);
if (! auth_result || auth_result["error"])
return false;

return true;
}

public bool ping()
{
map args = {
};
map result = send_request("cmd_ping", args, 10);
if (! result || result["error"])
return false;

return true;
}

public bool register_as_auth_agent(map agent_info)
{
map args = {
"server_id" : _config["server_id"],
"agent_info" : agent_info,
};
mixed register_result = send_request("cmd_auth_agent_register", args);
if (! register_result || register_result["error"])
return false;

return true;
}

public string get_name()
{
return _config["account"];
}

public mixed get_server_id()
{
return _config["server_id"];
}

mixed auth_connection_callback(object c)
{
map auth_para = {
"account" : _config["account"],
"server_id" : _config["server_id"],
} + _config["auth_account_para"];
mixed auth_result = c.send_request_priority("cmd_auth_account", auth_para);
if (! auth_result || auth_result["error"])
return false;

if (_config["act_as_agent"])
{
map register_args = {
"server_id" : _config["server_id"],
"agent_info" : _config["act_as_agent"],
};
mixed register_result = c.send_request_priority(conn, "cmd_auth_agent_register", register_args);
if (! register_result || register_result["error"])
return false;
}

return true;
}
// social_client.gs
// created by linlc oct/30/2020
// social_server连接客户端

#pragma parallel

import gs.lang.*;
import gs.util.*;

public object create_client(map para)
{
object ob = new_object(__DIR__ "SocialClient.gs", this_domain(), para);
return ob;
}