给 text server 发消息
<?php
Events
{
public static function onWorkerStart(BusinessWorker $worker)
{
var_dump(sprintf('%s #%s start', $worker->name, $worker->id));
Events::$worker = $worker;
Events::$text_worker = new Worker("text://0.0.0.0:5678");
Events::$text_worker->onMessage = function($connection, $data)
{
$args = func_get_args();
[$client_id, $message] = $args;
$data = Events::decode($message);
$eventType = $data['event_type'] ?? $data['type'] ?? null;
switch ($eventType) {
case 'room.add':
$number = data_get($data, 'data.number');
if (!$number) {
$connection->send('param number mast provider.');
}
break;
default:
$connection->send(downlink('error', null, 500, 'unknown event_type: '.$eventType));
break;
}
};
Events::$text_worker->listen();
// 初始化游戏大区的信息
Hall::init();
}
}
<?php
function sendToTextServer($type, $message, $host = null) {
if (!$host) {
return false;
}
$client = stream_socket_client($host, $err_code, $err_msg);
if (!$client) {
throw new \RuntimeException("stream_socket_client fail err_code={$err_code} err_msg={$err_msg}");
}
$buffer = json_encode([
'type' => $type,
'link' => 'uplink',
'data' => $message,
], \JSON_UNESCAPED_SLASHES|\JSON_UNESCAPED_UNICODE|\JSON_NUMERIC_CHECK);
$buffer .= "\n";
fwrite($client, $buffer);
$result = fread($client, 1024);
fclose($client);
return trim($result);
}
// $host = 'tcp://1.2.3.4:5678';
$host = 'tcp://172.30.80.3:5678';
$result = sendToTextServer('room.add', [
'number' => '123',
], $host);
var_dump($result);
个评论