Skip to content

Commit c2a794a

Browse files
v1.2.0 Release
Websocketクライアントの実装 デバッグログの削除 ライブラリのバージョンアップに伴う修正
1 parent 7a108c3 commit c2a794a

7 files changed

Lines changed: 1398 additions & 44 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
/**
3+
* SocketManager初期化クラスのファイル
4+
*
5+
* SocketManagerのsetInitSocketManagerメソッドへ引き渡される初期化クラスのファイル
6+
*/
7+
8+
namespace App\InitClass;
9+
10+
use SocketManager\Library\IInitSocketManager;
11+
use SocketManager\Library\SocketManagerParameter;
12+
13+
use App\UnitParameter\ParameterForWebsocket;
14+
15+
16+
/**
17+
* SocketManager初期化クラス
18+
*
19+
* IInitSocketManagerインタフェースをインプリメントする
20+
*/
21+
class InitForWebsocketClient implements IInitSocketManager
22+
{
23+
//--------------------------------------------------------------------------
24+
// 定数
25+
//--------------------------------------------------------------------------
26+
27+
28+
//--------------------------------------------------------------------------
29+
// プロパティ
30+
//--------------------------------------------------------------------------
31+
32+
/**
33+
* UNITパラメータインスタンス
34+
*/
35+
protected ?SocketManagerParameter $param = null;
36+
37+
/**
38+
* ポート番号
39+
*/
40+
protected ?int $port = null;
41+
42+
43+
//--------------------------------------------------------------------------
44+
// メソッド
45+
//--------------------------------------------------------------------------
46+
47+
/**
48+
* コンストラクタ
49+
*
50+
* @param SocketManagerParameter $p_param UNITパラメータ
51+
* @param int $p_port ポート番号
52+
*/
53+
public function __construct(SocketManagerParameter $p_param, int $p_port)
54+
{
55+
$this->param = $p_param;
56+
$this->port = $p_port;
57+
}
58+
59+
/**
60+
* ログライターの取得
61+
*
62+
* nullを返す場合は無効化(但し、ライブラリ内部で出力されているエラーメッセージも出力されない)
63+
*
64+
* @return mixed "function(string $p_level, array $p_param): void" or null(ログ出力なし)
65+
*/
66+
public function getLogWriter()
67+
{
68+
return function(string $p_level, array $p_param)
69+
{
70+
$filename = date('Ymd');
71+
$now = date('Y-m-d H:i:s');
72+
$log = $now." {$p_level} ".print_r($p_param, true)."\n";
73+
error_log($log, 3, "./logs/socket-manager-log/{$filename}_C{$this->port}.log");
74+
};
75+
}
76+
77+
/**
78+
* シリアライザーの取得
79+
*
80+
* nullを返す場合は無効化となる。
81+
* エラー発生時はUnitExceptionクラスで例外をスローして切断する。
82+
*
83+
* @return mixed "function(mixed $p_data): mixed" or null(変更なし)
84+
*/
85+
public function getSerializer()
86+
{
87+
return function($p_data)
88+
{
89+
$p_data['data'] = json_encode($p_data['data']);
90+
return $p_data;
91+
};
92+
}
93+
94+
/**
95+
* アンシリアライザーの取得
96+
*
97+
* nullを返す場合は無効化となる。
98+
* エラー発生時はUnitExceptionクラスで例外をスローして切断する。
99+
*
100+
* @return mixed "function(mixed $p_data): mixed" or null(変更なし)
101+
*/
102+
public function getUnserializer()
103+
{
104+
return function($p_data)
105+
{
106+
$p_data['data'] = json_decode($p_data['data'], true);
107+
return $p_data;
108+
};
109+
}
110+
111+
/**
112+
* コマンドディスパッチャーの取得
113+
*
114+
* 受信データからコマンドを解析して返す
115+
*
116+
* コマンドUNIT実行中に受信データが溜まっていた場合でもコマンドUNITの処理が完了するまで
117+
* 待ってから起動されるため処理競合の調停役を兼ねる
118+
*
119+
* nullを返す場合は無効化となる。エラー発生時はUnitExceptionクラスで例外をスローして切断する。
120+
*
121+
* @return mixed "function(SocketManagerParameter $p_param, mixed $p_dat): ?string" or null(変更なし)
122+
*/
123+
public function getCommandDispatcher()
124+
{
125+
return function(ParameterForWebsocket $p_param, $p_dat): ?string
126+
{
127+
/**
128+
* (注意)切断フレーム実装の都合上"data"キーは送受信データに必ず含めて下さい。
129+
*/
130+
return $p_dat['data']['cmd'];
131+
};
132+
}
133+
134+
/**
135+
* 緊急停止時のコールバックの取得
136+
*
137+
* 例外等の緊急切断時に実行される。nullを返す場合は無効化となる。
138+
*
139+
* @return mixed "function(SocketManagerParameter $p_param)"
140+
*/
141+
public function getEmergencyCallback()
142+
{
143+
return null;
144+
}
145+
146+
/**
147+
* UNITパラメータインスタンスの取得
148+
*
149+
* nullの場合はSocketManagerParameterのインスタンスが適用される
150+
*
151+
* @return ?SocketManagerParameter SocketManagerParameterクラスのインスタンス(※1)
152+
* @see:RETURN (※1)当該クラス、あるいは当該クラスを継承したクラスも指定可
153+
*/
154+
public function getUnitParameter(): ?SocketManagerParameter
155+
{
156+
return $this->param;
157+
}
158+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* メイン処理クラスのファイル
4+
*
5+
* SocketManagerの実行
6+
*/
7+
8+
namespace App\MainClass;
9+
10+
use SocketManager\Library\SocketManager;
11+
use SocketManager\Library\FrameWork\Console;
12+
13+
use App\InitClass\InitForWebsocketClient;
14+
use App\ProtocolUnits\ProtocolForWebsocketClient;
15+
use App\UnitParameter\ParameterForWebsocketClient;
16+
17+
18+
/**
19+
* メイン処理クラス
20+
*
21+
* SocketManagerの初期化と実行
22+
*/
23+
class MainForWebsocketClient extends Console
24+
{
25+
/**
26+
* @var string $identifer サーバー識別子
27+
*/
28+
protected string $identifer = 'app:websocket-client {port_no?}';
29+
30+
/**
31+
* @var string $description コマンド説明
32+
*/
33+
protected string $description = 'Websocketクライアント';
34+
35+
/**
36+
* @var string $host ホスト名(接続用)
37+
*/
38+
private string $host = 'localhost';
39+
40+
/**
41+
* @var int $port ポート番号(接続用)
42+
*/
43+
private int $port = 10000;
44+
45+
/**
46+
* @var int $cycle_interval 周期インターバル時間(μs)
47+
*/
48+
private int $cycle_interval = 1000;
49+
50+
/**
51+
* @var int $alive_interval アライブチェックタイムアウト時間(s)
52+
*/
53+
private int $alive_interval = 3600;
54+
55+
56+
/**
57+
* サーバー起動
58+
*
59+
*/
60+
public function exec()
61+
{
62+
//--------------------------------------------------------------------------
63+
// 設定値の反映
64+
//--------------------------------------------------------------------------
65+
66+
// ホスト名の設定
67+
$this->host = config('const.host', $this->host);
68+
69+
// ポート番号の設定
70+
$this->port = config('const.port', $this->port);
71+
72+
// 周期インターバルの設定
73+
$this->cycle_interval = config('const.cycle_interval', $this->cycle_interval);
74+
75+
// アライブチェックタイムアウト時間の設定
76+
$this->alive_interval = config('const.alive_interval', $this->alive_interval);
77+
78+
//--------------------------------------------------------------------------
79+
// 引数の反映
80+
//--------------------------------------------------------------------------
81+
82+
// 引数の取得
83+
$port = $this->getParameter('port_no');
84+
if($port !== null)
85+
{
86+
$this->port = $port;
87+
}
88+
89+
//--------------------------------------------------------------------------
90+
// SocketManagerの初期化
91+
//--------------------------------------------------------------------------
92+
93+
// ソケットマネージャーのインスタンス設定
94+
$manager = new SocketManager();
95+
96+
// UNITパラメータインスタンスの設定
97+
$param = new ParameterForWebsocketClient();
98+
99+
// SocketManagerの設定値初期設定
100+
$init = new InitForWebsocketClient($param, $this->port);
101+
$manager->setInitSocketManager($init);
102+
103+
// プロトコルUNITの設定
104+
$entry = new ProtocolForWebsocketClient();
105+
$manager->setProtocolUnits($entry);
106+
107+
//--------------------------------------------------------------------------
108+
// 接続開始
109+
//--------------------------------------------------------------------------
110+
111+
$ret = $manager->connect($this->host, $this->port);
112+
if($ret === false)
113+
{
114+
goto finish; // リッスン失敗
115+
}
116+
117+
//--------------------------------------------------------------------------
118+
// ノンブロッキングループ
119+
//--------------------------------------------------------------------------
120+
121+
while(true)
122+
{
123+
// 周期ドリブン
124+
$ret = $manager->cycleDriven($this->cycle_interval, $this->alive_interval);
125+
if($ret === false)
126+
{
127+
goto finish;
128+
}
129+
}
130+
131+
finish:
132+
// 全接続クローズ
133+
$manager->shutdownAll();
134+
}
135+
}

0 commit comments

Comments
 (0)