-
Notifications
You must be signed in to change notification settings - Fork 33
notification en
Comet Service Notifications contains Comet Monitor Notifications and Comet Notifications. All notifications will requeste to the rpc_api_url
address which in the configuration file (such as comet.ini), and the request mothod is POST
. To ensure the validity of the request, the http request header User-Agent
will be send. User-Agent
is the rpc_user_agent
string in the configuration. The request body is a json string. The json format is as follows:
{
"class": "Im", //string, always is "Im"
"method": "", //string, notification type,see below for more details
"args": [ //array, notification parameters
arg1,
arg2
]
}
Accepting request example in PHP:
$agent = 'CtxImRpc 1.0';
$body = file_get_contents('php://input');
$data = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE || ! is_array($data)) {
throw new \Exception('bad request' . var_export($body, true));
}
if (isset($_SERVER['HTTP_USER_AGENT']) &&
$_SERVER['HTTP_USER_AGENT'] == $agent &&
isset($data['class'], $data['method'])
) {
$class = $data['class'];
$method = $data['method'];
$args = isset($data['args']) ? $data['args'] : array();
header('Content-Type: application/json; charset=utf-8');
$im = new $class;
$data = call_user_func_array(array($im, $method), $args);
return $this->success($data);
} else {
throw new \Exception("bad request");
}
The Comet Monitor service mainly monitors the running of the Comet service. The notifications include two types:
In the following two cases, Comet Monitor will notify the application that Comet Service having came online.
- The Comet Monitor service is running when the Comet service comes online.
- When the Comet Monitor service is just started, there is already one or more running Comet services.
{
"class": "Im",
"method": "addCometServer",
"args": [
node, //string, node,/comet/cometRpcAddr,like: /comet/192.168.3.165:8901
revision //big64, The only notification version number, To avoid the application processe incorrectly, when the multiple monitors are running the message is repeatedly sent to application.
]
}
Comet Monitor Service doesn't care about the return value of the application service, as it is successfully processed.
When the Comet Service go offline, Comet Monitor will notify the application service that Comet Service have gone offline.
{
"class": "Im",
"method": "removeCometServer",
"args": [
node, //string, node,/comet/cometRpcAddr,like: /comet/192.168.3.165:8901
revision //big64, The only notification version number, To avoid the application processe incorrectly, when the multiple monitors are running the message is repeatedly sent to application.
]
}
Comet Monitor Service doesn't care about the return value of the application service, as it is successfully processed.
Comet Service acts as the access layer for long connections, handing all long connections. Before the client connects to the Comet Service, you need to apply connection token to the application service. So that when the client connects to the Comet service check the token is correct. The websocket url format generation rule for the client to connect with the Comet service is {protocol}:://{cometRpcAddr}/ws?c={connId}&t={token}&i={clientInfo}
, for example: ws://comet.demo.com:8900/ ws?c=conn-id-123&t=token-abc&i=client-info-xx
, PHP code like:
/**
* @var $connId, string, the unique id assigned to the client, such as uid + platform(pc/mobile) + uniqid
* @var $token, string, token
* @var $clientInfo, string, custom client related information, it will be passed when client online or offline
*/
$url = sprintf('ws://comet.demo.com:8900/ws?c=%s&t=%s&i=%s', $connId, $token, $clientInfo);
Comet Service Notifications include two types:
When the client connects to the Comet service, the Comet Service requests the application service to verify that the connection is valid.
{
"class": "Im",
"method": "checkToken",
"args": [
ConnId, //string, ConnId
Token, //string, token
ClientInfo, //string, custom client related information
cometRpcAddr //string, The rpc address of the comet service connected by the client, like: 192.168.3.165:8901, so that the application service can verify whether it is connected to the specified comet service during custom load balancing.
]
}
Notice: With this notification request, the Comet Service cares about the response of the application service:
- response http code must be 200
- response body format must be json,format as below:
{
"code": 0, //int, must be 0
"data": true,
"error": "", //string, error info
}
If the response of the above requirements is not met, the Comet Service considers that the client token verification failed, and the connection will be not allowed.
When the client comes online, Comet will notify the application service that the client connects to the Comet service and the connection is successful.
{
"class": "Im",
"method": "online",
"args": [
ConnId, //ConnId
ClientInfo, //string, custom client related information
cometRpcAddr //current client comet service rpc address
]
}
Comet Monitor Service doesn't care about the return value of the application service, as it is successfully processed.
When the client goes offline, Comet will notify the application service that the client has successfully disconnected from the Comet service.
{
"class": "Im",
"method": "offline",
"args": [
ConnId, //ConnId
ClientInfo, //string, custom client related information
cometRpcAddr //current client comet service rpc address
]
}
Comet Monitor Service doesn't care about the return value of the application service, as it is successfully processed.
^_^ 2018.