このエントリでは、C#クライアントSDKを用いてDiarkis Cloudへ接続する方法を解説します。
下記の図は、Diarkis Cloudとデバイスおよびアプリケーションサーバとの接続関係を示した構成図です。

接続エンドポイントの取得
Diarkis C#クライアントを使用するには、TCP及びUDP/RUDPエンドポイントを取得する必要があります。
エンドポイントは、Diarkis Cloudが提供するREST APIエンドポイントから取得することができます。
curl https://xxx.diarkis.io/auth/:userID -h "ClientKey:zzz"
REST APIエンドポイントは、WebコンソールからDiarkis Cloudのクラスタを展開すると生成されます。
クラスタの展開方法は こちら のエントリをご覧ください。
実サービス環境でアプリケーションを提供する場合、ユーザのデータや認証などを管理するために、前項の図にあるようなDiarkis Cloudとは別のアプリケーションサーバを用意することを推奨します。
REST APIでは、リクエスト元のクライアントを一意に識別するために、リクエストURIにユーザーIDを指定する必要があります。
ユーザーIDは、接続元のアプリケーションで指定してください。
REST APIでは、リクエストヘッダーに “ClientKey” を指定する必要があります。
“ClientKey” に指定する値は、Diarkis CloudのWebコンソールから取得することができます。
リクエストが成功すると、以下の例のようなJSON形式のレスポンスが返されます。
{
"TCP":"xxxxxx.diarkis.io:7200",
"UDP":"yyyyyy.diarkis.io:7100",
"sid":"9985a8943ce5467a8f9436b0a38a7a25",
"encryptionKey":"cdca9e1b31bb4cf5ad4072a02fb7c0ad",
"encryptionIV":"1706e3f72e934d6c939b15cf436eb9e6",
"encryptionMacKey":"f2a99106188d478fbe1537b6f2e071c8"
}
イベント発火のために Update メソッドを呼ぶ
Diarkis の TCP、UDP/RUDP クライアントはネットワークスレッドからのイベントを発火させるために Update メソッドを呼ぶ必要があります。
Unity ゲームエンジンを使っている場合は以下のように Unity の Update メソッドの中で呼ぶ必要があります。
// This is the Unity Update method - This is called on frame update
private void Update()
{
if (diarkisUDPClient != null)
{
diarkisUDPClient.Update();
}
if (diarkisTCPClient != null)
{
diarkisTCPClient.Update();
}
}
UDP/RUDPで接続する
エンドポイント情報を取得することで、Diarkis Cloudへ接続する準備が整いました。
以下の例は、パケット暗号化キー(sid, encryptionKey, encryptionIV, encryptionMacKey)を用いて接続するものです。
注: UDPには厳密には “接続” という概念がありませんが、ここでは便宜上 “接続” という用語を使用しています。
int sendInterval = 200; // This is the interval in milliseconds to send packets. All packets that fall within this interval will be combined
int echoInterval = 5000; // The default is 5000 milliseconds
Diarkis.Udp udp = new Diarkis.Udp(sendInterval, echoInterval);
udp.SetClientKey(clientKey);
udp.SetEncryptionKeys(sid, key, iv, mackey);
// This event will raise when the connection is successfull
udp.OnConnect += HandleOnConnect;
// All network related errors will be caught here
udp.OnException += HandleOnException;
// Start the connection. This will raise either OnConnect or OnException event
udp.Connect(addr, port);
private void HandleOnConnect(bool reconnected)
{
// reconnected will be true, if re-connected
// Start your awesome real-time application here
}
private void HandleOnException(int code, string msg)
{
// We handle error with network here...
}
UDP接続を終了する
以下の例は、UDP接続とそのイベントリスナーを終了する方法です。
// This event is raised when the client is disconnected
udp.OnDisconnect += HandleOnDisconnect;
udp.Disconnect();
private void HandleOnDisconnect(bool reconnecting)
{
// reconnecting will be true, if Disconnect is invcoked by re-connection
}
TCPで接続する
以下の例は、パケット暗号化キー(sid, encryptionKey, encryptionIV, encryptionMacKey)を用いてTCP接続するものです。
int heartbeatInterval = 5000; // The default is 5000 milliseconds
Diarkis.Tcp tcp = new Diarkis.Tcp(heartbeatInterval);
tcp.SetClientKey(clientKey);
tcp.SetEncryptionKeys(sid, key, iv, mackey);
// This event will raise when the connection is successfull
tcp.OnConnect += HandleOnConnect;
// All network related errors will be caught here
tcp.OnException += HandleOnException;
// Start the connection. This will raise either OnConnect or OnException event
tcp.Connect(addr, port);
private void HandleOnConnect(bool reconnected)
{
// reconnected will be true, if re-connected
// Start your awesome real-time application here
}
private void HandleOnException(int code, string msg)
{
// We handle error with network here...
}
TCP接続を終了する
以下の例は、TCP接続とそのイベントリスナーを終了する方法です。
// This event is raised when the client is disconnected
tcp.OnDisconnect += HandleOnDisconnect;
tcp.Disconnect();
private void HandleOnDisconnect(bool reconnecting)
{
// reconnecting will be true, if Disconnect is invcoked by re-connection
}
おわりに
本エントリでは、Diarkis C# クライアントSDKを用いて、Diarkis Cloudとのリアルタイム通信を実装する方法を紹介しました。
Diarkisは標準でパケットを暗号化する方法を提供しているため、開発者は通信内容の暗号化を気にせず、アプリケーションの開発に集中できます。