我们有一个用例,其中用户尚未登录应用程序,并且尚未创建身份验证令牌。该应用程序需要通过公共通道连接到Web服务器,以检查应用程序更新。连接失败。具有以下错误原因:“未知错误;代码:500; HttpStatus代码:(404)NotFound。

Nuget 0.8.11中的Ably库和SDK。

以下代码未连接到Web服务器。

    public ExtendedAblyIoClient(string name, string ClientId, string ChannelId, string AuthUrl, string ablyKey)
    {
        _name = name;
        _authUrl = AuthUrl;
        _clientId = ClientId;
        _channelId = ChannelId;
        _ablyAppKey = ablyKey;
        _authUri = new Uri(_authUrl);  // local host for testing and development.
        _httpRequestTime = TimeSpan.FromHours(2.0);

        ClientOptions clientOptions = new ClientOptions
        {
            Key = _ablyAppKey,
            ClientId = _clientId,
            AuthUrl = _authUri,
            Tls = false,
            HttpRequestTimeout = _httpRequestTime,
            HttpOpenTimeout = _httpRequestTime
        };

        commonInitialization(clientOptions);
        _channel = _ablyClient.Channels.Get(_channelId);
        _channel.Subscribe(message =>
        {
            OnMessageCallback(_sender, _channelId, message.Data.ToString());
        });

    }

    private void commonInitialization(ClientOptions clientOptions)
    {
        _ablyClient = new AblyRealtime(clientOptions);

        _ablyClient.Connection.On(ConnectionState.Connected, args =>
        {
            realTimeClientOnConnected(_sender);
        });

        _ablyClient.Connection.On(ConnectionState.Disconnected, args =>
        {
            realTimeClientOnDisconnected(_sender);
        });
        _ablyClient.Connection.On(ConnectionState.Failed, args =>
        {
            string WhyError = _name + " Failed: " + _ablyClient.ToString();
            realTimeClientOnDisconnected(WhyError);
        });
    }

最佳答案

404将从客户端库尝试与您指定的AuthUrl联系,并获取404。

从您的问题看来,如果您希望连接到“公共频道”,则不必进行身份验证。这不是Ably auth的工作方式;连接到您的应用程序的任何用户都需要进行身份验证,并具有与之连接的令牌或api密钥,这就是要进行身份验证的意思。

如果您不希望它们连接到不在public:名称空间中的任何通道,并且仅具有这些通道的订阅功能,则可以给令牌设置功能设置为{"public:*":["subscribe"]}。但是您仍然需要给他们一个令牌。

通读https://www.ably.io/documentation/general/authentication以获得有关Ably身份验证模型的文档,并通读https://www.ably.io/documentation/realtime/authentication专门用于认证实时连接。

编辑:您指出您也正在传递密钥。客户端不能同时使用两者-它要么与密钥连接,要么从authUrl获取令牌并与其连接-因此,它可能只是忽略其中之一。删除您不想使用的那个。

编辑:我也建议删除禁用Tls的选项并更改http超时,并将其保留为默认值。除非出于特殊原因要禁用tls,否则出于安全原因,我们强烈建议将其禁用。

09-27 01:34
查看更多