facebook twitter hatena line email

「Unity/Agora/voicechat community/token取得」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
("CAN_NOT_GET_GATEWAY_SERVER"エラーとなる場合)
("enewToken should not be called before user join"エラーとなる場合)
行66: 行66:
 
JoinChannel()前に、SetClientRole()を実行してないため起こる。JoinChannel()前に、SetClientRole()を実行すれば良い。
 
JoinChannel()前に、SetClientRole()を実行してないため起こる。JoinChannel()前に、SetClientRole()を実行すれば良い。
  
==="enewToken should not be called before user join"エラーとなる場合===
+
==="renewToken should not be called before user join"エラーとなる場合===
 
エラー詳細
 
エラー詳細
 
  AgoraRTCException: AgoraRTCError INVALID_OPERATION: renewToken should not be called before user join
 
  AgoraRTCException: AgoraRTCError INVALID_OPERATION: renewToken should not be called before user join
 
*参加前にrenewTokenを使ってるため。今回は、SetClientRoleをJoin前に使ってたので、SetClientRoleを、Join以後に行うように。
 
*参加前にrenewTokenを使ってるため。今回は、SetClientRoleをJoin前に使ってたので、SetClientRoleを、Join以後に行うように。
 +
 +
==="Cannot read properties of undefined"エラーとなる場合===
 +
エラー詳細
 +
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'toString')
 +
本番のAppIDで、useTokenを、falseにしてると、このようなエラーが出るので、AppIdをテスト用に。
  
 
==mRtcEngine.JoinChannel()部分をtoken取得コードに変更==
 
==mRtcEngine.JoinChannel()部分をtoken取得コードに変更==

2022年12月16日 (金) 17:38時点における版

1日有効な本番token取得

  1. https://console.agora.io/ にアクセス
  2. stageをliveに
  3. Generate temp RTC tokenを選択して、チャンネル名を入れてtokenを取得
  4. サンプルのClientManagerTestシーンの、CanvasVideoのInspectorにあるuseTokenを、falseにして、token文字列を設置するとつながる

本番tokenをワンタイムtoken経由で取得する

  1. サンプルのClientManagerTestシーンだと、"VideoCanvas"のGameObjectとかに、TokenClientを、AddComponentする
  2. ServerURLを入れる。(ttp://localhost:8080/ な感じ。この場合ttp://localhost:8080/rte/testing/publisher/uid/1234/?3600 にアクセスされる)

useTokenをtrueにすることでエラーになる場合

ArgumentNullException: String reference not set to an instance of a String.
Parameter name: source

TokenClientを、どこかのGameObjectにAddComponentしてServerURLを設定すれば良い。

token文字列が取得できない場合

token取得用apiと、unity設置してるサイトのドメインが同じでない場合は、network.errorになるので、ドメインは、同じにする。

golangでワンタイムtokenを設定

https://github.com/AgoraIO-Community/agora-token-service

agora-token-service設定

$ git clone https://github.com/AgoraIO-Community/agora-token-service.git
$ cp .env.example .env

.envを設定

APP_ID=
APP_CERTIFICATE=
$ go run cmd/main.go

phpでワンタイムtokenを取得する

git clone https://github.com/AgoraIO/Tools.git
$ agora-token/Tools/DynamicKey/AgoraDynamicKey/php をサーバーへ設置する。
  1. RtcTokenBuilderSample.phpを確認する
  2. serverURLにttp://localhost:8080/php/sample/RtcTokenBuilderSample.php?を設置する
  3. 処理すると、ttp://localhost:8080/php/sample/RtcTokenBuilderSample.php?/rtc/unity3d/publisher/uid/0/?expiry=3600 が実行される。
  4. このときrtcとuidの取得方法は、以下の通り。
if (preg_match("!/rtc/([\d\w]+)!", $_SERVER['QUERY_STRING'], $matches)) {
    $channelName = $matches[1];
}
if (preg_match("!/uid/([\d\w]+)!", $_SERVER['QUERY_STRING'], $matches)) {
    $uid = $matches[1];
    $uidStr = $matches[1];
}
if (preg_match("!/publisher/!", $_SERVER['QUERY_STRING'], $matches)) {
    $role = RtcTokenBuilder::RolePublisher;
} else if (preg_match("!/subscriber/!", $_SERVER['QUERY_STRING'], $matches)) {
    $role = RtcTokenBuilder::RoleSubscriber;
}

戻り値はjsonで

echo json_encode(array('rtcToken' => $token, 'rtmToken' => $token));

"CAN_NOT_GET_GATEWAY_SERVER"エラーとなる場合

エラー詳細

Agora-SDK [ERROR]: [client-1513c] join number: 1, Joining channel failed, rollback AgoraRTCException: AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: flag: 4096, message: AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: invalid token, authorized failed
  • AccessToken2Sample.phpではなく、RtcTokenBuilderSample.phpの方を使う。
  • token文字列がおかしいので、channelNameがちゃんと設定されてるか確認する。

"audience can not publish stream"エラーとなる場合

エラー詳細

Uncaught (in promise) AgoraRTCException: AgoraRTCError INVALID_OPERATION: audience can not publish stream

JoinChannel()前に、SetClientRole()を実行してないため起こる。JoinChannel()前に、SetClientRole()を実行すれば良い。

"renewToken should not be called before user join"エラーとなる場合

エラー詳細

AgoraRTCException: AgoraRTCError INVALID_OPERATION: renewToken should not be called before user join
  • 参加前にrenewTokenを使ってるため。今回は、SetClientRoleをJoin前に使ってたので、SetClientRoleを、Join以後に行うように。

"Cannot read properties of undefined"エラーとなる場合

エラー詳細

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'toString')

本番のAppIDで、useTokenを、falseにしてると、このようなエラーが出るので、AppIdをテスト用に。

mRtcEngine.JoinChannel()部分をtoken取得コードに変更

string token = "";
if (!useToken)
{
    mRtcEngine.JoinChannel(token, CHANNEL_NAME_1, "", 0, new ChannelMediaOptions(true, true, true, true));
}
else
{
    TokenClient.Instance.SetRtcEngineInstance(mRtcEngine);
    TokenClient.Instance.GetRtcToken(CHANNEL_NAME_1, 0, (token) =>
    {
        mRtcEngine.JoinChannel(token, CHANNEL_NAME_1, "", 0, new ChannelMediaOptions(true, true, true, true));
    });
}

別チャンネルを作ってる場合

チャンネル名(例:"CHANNEL_NAME_VIDEO")をあわせる。

channelVideo = mRtcEngine.CreateChannel(CHANNEL_NAME_VIDEO);
if (!useToken)
{
    channelVideo.JoinChannel(token, "", 0, new ChannelMediaOptions(true, true));
}
else
{
    TokenClient.Instance.SetRtcEngineInstance(mRtcEngine);
    TokenClient.Instance.GetRtcToken(CHANNEL_NAME_VIDEO, 0, (token) =>
    {
        channelVideo.JoinChannel(token, "", 0, new ChannelMediaOptions(true, true));
    });
}