Tutorial 6 - Group
最終更新
役に立ちましたか?
役に立ちましたか?
private const string HOST = "127.0.0.1:7000";
private const string CLIENT_KEY = "";
private const string UID = ""; // 空文字の場合はランダム生成DiarkisEventHandler handler = diarkis.EventHandler;
handler.OnUDPConnect(OnConnect, this);
handler.OnUDPDisconnect(OnDisconnect, this);
handler.OnUDPFail(_ => SetNetworkState("接続失敗", ColorRed), this);
handler.OnHttpError(_ => SetNetworkState("HTTP 認証エラー", ColorRed), this);
handler.OnGroupCreate(OnGroupCreated, this);
handler.OnGroupJoin(OnGroupJoined, this);
handler.OnGroupLeave(OnGroupLeft, this);
handler.OnGroupMemberJoin(_ => AddChatLine("[システム] メンバーが参加しました"), this);
handler.OnGroupMemberLeave(_ => AddChatLine("[システム] メンバーが退出しました"), this);
handler.OnGroupMemberBroadcast(OnBroadcastReceived, this);private void OnJoinClicked()
{
string groupID = _groupIDInput.text.Trim();
if (string.IsNullOrEmpty(groupID)) return;
DiarkisNetworkManager.GetDiarkisInterface(INTERFACE_NAME).Group.SendJoin(groupID);
}private void OnGroupCreated(DiarkisGroupEventArgs e)
{
if (!e.IsSuccess()) { AddChatLine($"[エラー] {e.GetErrorMessage()}"); return; }
_currentGroupID = e.GetGroupID();
_inGroup = true;
AddChatLine($"[システム] グループを作成しました: {_currentGroupID}");
RefreshButtons();
}
private void OnGroupJoined(DiarkisGroupEventArgs e)
{
if (!e.IsSuccess()) { AddChatLine($"[エラー] {e.GetErrorMessage()}"); return; }
_currentGroupID = e.GetGroupID();
_inGroup = true;
AddChatLine($"[システム] グループに参加しました: {_currentGroupID}");
RefreshButtons();
}private void OnMessageClicked(string message)
{
if (!_inGroup) return;
byte[] bytes = Encoding.UTF8.GetBytes(message);
DiarkisNetworkManager.GetDiarkisInterface(INTERFACE_NAME).Group.SendBroadcast(_currentGroupID, bytes);
// ブロードキャストは送信者自身には届かないため、ローカルに表示する
AddChatLine($"[自分] {message}");
}private void OnBroadcastReceived(DiarkisPayloadEventArgs e)
{
DiarkisByteVector payload = e.GetPayload();
if (payload == null || payload.Count == 0) return;
byte[] bytes = new byte[payload.Count];
for (int i = 0; i < payload.Count; i++)
bytes[i] = payload[i];
string message = Encoding.UTF8.GetString(bytes);
AddChatLine($"[受信] {message}");
}