加入通话时获取CAN_NOT_GET_GATEWAY_SERVER异常

问题描述

我正在尝试使用agora发起视频通话,并且在控制台上引发跟随错误,这是在我尝试加入通话时发生的。

code: "CAN_NOT_GET_GATEWAY_SERVER"
data: {retry: false}
message: "AgoraRTCError CAN_NOT_GET_GATEWAY_SERVER: dynamic use static key"
name: "AgoraRTCException"

在这里附上了我正在使用Agora的react js模块的react JS代码,并遵循了docs,我已经验证了电子邮件和创建的正确APP,它与Agora的Web演示一起使用,但不在此处。请帮忙

import React,{ useEffect,useState,useRef } from "react";
import ReactDOM from "react-dom";
import "./App.css";
import { options,rtc } from "./constants";
import AgoraRTC from "agora-rtc-sdk-ng";

function App() {
  async function handleSubmit(e) {
    try {
      if (channelRef.current.value === "") {
        return console.log("Please Enter Channel Name");
      }

      setJoined(true);

      rtc.client = AgoraRTC.createClient({ mode: "rtc",codec: "h264" });
      const uid = await rtc.client.join(
        options.appId,channelRef.current.value,options.token,null
      );

      // Create an audio track from the audio captured by a microphone
      rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();
      // Create a video track from the video captured by a camera
      rtc.localVideoTrack = await AgoraRTC.createCameravideoTrack();

      rtc.localVideoTrack.play("local-stream");

      rtc.client.on("user-published",async (user,mediaType) => {
        // Subscribe to a remote user
        await rtc.client.subscribe(user);
        console.log("subscribe success");
        // console.log(user);

        if (mediaType === "video" || mediaType === "all") {
          // Get `RemoteVideoTrack` in the `user` object.
          const remoteVideoTrack = user.videoTrack;
          console.log(remoteVideoTrack);

          // Dynamically create a container in the form of a DIV element for playing the remote video track.
          const PlayerContainer = React.createElement("div",{
            id: user.uid,className: "stream",});
          ReactDOM.render(
            PlayerContainer,document.getElementById("remote-stream")
          );

          user.videoTrack.play(`${user.uid}`);
        }

        if (mediaType === "audio" || mediaType === "all") {
          // Get `RemoteAudioTrack` in the `user` object.
          const remoteAudioTrack = user.audioTrack;
          // Play the audio track. Do not need to pass any DOM element
          remoteAudioTrack.play();
        }
      });

      rtc.client.on("user-unpublished",(user) => {
        // Get the dynamically created DIV container
        const playerContainer = document.getElementById(user.uid);
        console.log(playerContainer);
        // Destroy the container
        playerContainer.remove();
      });

      // Publish the local audio and video tracks to the channel
      await rtc.client.publish([rtc.localAudioTrack,rtc.localVideoTrack]);

      console.log("publish success!");
    } catch (error) {
      console.error(error);
    }
  }

  async function handleLeave() {
    try {
      const localContainer = document.getElementById("local-stream");

      rtc.localAudioTrack.close();
      rtc.localVideoTrack.close();

      setJoined(false);
      localContainer.textContent = "";

      // Traverse all remote users
      rtc.client.remoteUsers.forEach((user) => {
        // Destroy the dynamically created DIV container
        const playerContainer = document.getElementById(user.uid);
        playerContainer && playerContainer.remove();
      });

      // Leave the channel
      await rtc.client.leave();
    } catch (err) {
      console.error(err);
    }
  }
  const [joined,setJoined] = useState(false);
  const channelRef = useRef("");
  const remoteRef = useRef("");
  const leaveRef = useRef("");

  return (
    <>
      <div className="container">
        <input
          type="text"
          ref={channelRef}
          id="channel"
          placeholder="Enter Channel name"
        />
        <input
          type="submit"
          value="Join"
          onClick={handleSubmit}
          disabled={joined ? true : false}
        />
        <input
          type="button"
          ref={leaveRef}
          value="Leave"
          onClick={handleLeave}
          disabled={joined ? false : true}
        />
      </div>
      {joined ? (
        <>
          <div id="local-stream" className="stream local-stream"></div>
          <div
            id="remote-stream"
            ref={remoteRef}
            className="stream remote-stream"
          ></div>
        </>
      ) : null}
    </>
  );
}

export default App;

解决方法

由于令牌过期,您需要在选项对象中再次设置令牌和频道名称。您可以从 https://console.agora.io/project/ 生成新令牌 enter image description here

,

与示例相关的文档说令牌是可选的,但实际上是必需的,这很奇怪。添加了它,问题就解决了

,

此问题的另一个可能原因可能是用于生成令牌的 uid 与用于加入频道的 uid 不匹配。因此,请确保令牌相同。

,

如果您使用的是 agora sdk 并且会发生这种情况 你一定要试试这些

  1. 启用项目的实时模式
  2. 启用二级证书并使用该证书
  3. 最后也是最重要的,如果您遇到令牌无效或身份验证失败的错误,则在 Uid 中输入 0 并生成 body: Stack( children: [ Center( child: Row(mainAxisAlignment: MainAxisAlignment.center,children: [ Image.asset( 'assets/pictalio_logo.png',width: 100,),Text( "Pictalio",style: TextStyle( fontFamily: "Sen",fontWeight: FontWeight.w700,fontSize: 30),]),Column( mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.end,children: <Widget>[ Container( margin: EdgeInsets.symmetric(horizontal: 10.0),width: double.infinity,child: TextButton( child: Text('Signup with my email',style: TextStyle(fontWeight: FontWeight.w700)),style: TextButton.styleFrom( backgroundColor: pinkColorContainer.color,primary: textColorContainer.color,shape: const BeveledRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(5))),onPressed: () { print('Pressed'); },)),Container( margin: EdgeInsets.symmetric(horizontal: 10.0),child: TextButton( child: Text('Signup with my google',RichText( text: TextSpan( text: "Already have an account?",style: TextStyle( color: Colors.black,fontWeight: FontWeight.w700),children: <TextSpan>[ TextSpan( text: ' Sign in!',style: TextStyle( color: textColorContainer.color,fontWeight: FontWeight.w700)),],) ],)

解决了我的问题,希望对你有帮助