javascript - SignalR - 连接启动失败

标签 javascript azure signalr azure-functions

我尝试运行Anthony Chu提供的教程。 https://www.youtube.com/watch?v=4d0wor7uAgQ

他使用 Azure CosmosDB、Azure Functions 和 SignalR 实现了“SimpleChat-Application”。您可以在其中实时在客户端之间发送消息。 我尝试了不同的设置和版本,但无法使其工作。

我的“index.html”中的源代码:(JavaScript)

  <script src="https://unpkg.com/@aspnet/signalr@1.0.0-rc1-final/dist/browser/signalr.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  
  <script>
    const apiBaseUrl = 'http://localhost:7071';
    const hubName = 'chat';
    getConnectionInfo().then(info => {      
      let username;
      while (!username && username !== null) {
        username = prompt('Enter a username');
      }
      console.log(info.accessTokenFactory);
      if (username === null) return;
      document.body.classList.add('ready');
      const messageForm = document.getElementById('message-form');
      const messageBox = document.getElementById('message-box');
      const messages = document.getElementById('messages');
      const options = {
        accessTokenFactory: () => info.accessKey
      };

      const connection = new signalR.HubConnectionBuilder()
        .withUrl(info.url, options)
        .configureLogging(signalR.LogLevel.Trace)
        .build();

      connection.onclose(() => console.log('disconnected'));

      console.log('retrieving messages');

      getMessage().then(messages => {
        for(let m of messages) {
          newMessage(m);
        }
        console.log('connecting...');
        connection.start()
          .then(() => console.log('connected!'))
          .catch(console.error);
      });
      console.log('connected after');
    }).catch(alert);
    
    function getConnectionInfo() {
      console.log("test");
      return axios.post(`${apiBaseUrl}/api/negotiate`)
        .then(resp => resp.data);
    }

    function getMessage(sender, messageText) {
      return axios.get(`${apiBaseUrl}/api/getmessage`).then(resp => resp.data);
    }

    function newMessage(message) {
      const newMessage = document.createElement('li');
      newMessage.appendChild(document.createTextNode(`${message.sender}: ${message.text}`));
      messages.prepend(newMessage);
    }
  </script>

源代码(协商 - function.json)

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "signalRConnectionInfo",
      "name": "connectionInfo",
      "hubName": "chat",
      "connectionStringSetting": "AzureSignalRConnectionString", 
      "direction": "in"
    }
  ]
}

源代码(协商 - index.js)

module.exports = function (context, req, connectionInfo) {
    context.res = { body: connectionInfo };
    context.done();
}

我可以从 cosmosDB 接收数据,但是当我使用 connection.start() 方法时...它没有连接到服务?!

有人可以帮我吗?

非常感谢!!!

版本:

  • 函数(2.2.70)

  • Microsoft.Azure.WebJobs.Extensions.SignalRService (1.0.0-preview1-10002)

  • Microsoft.Azure.WebJobs.Extensions.CosmosDB (3.0.2)

  • Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator (1.0.1)

(“Me: Hallo”是 CosmosDB 中的一个条目)

Result Page with Console-Log

最佳答案

请注意这部分代码...

      const options = {
        accessTokenFactory: () => info.accessKey
      };

      const connection = new signalR.HubConnectionBuilder()
        .withUrl(info.url, options)
        .configureLogging(signalR.LogLevel.Trace)
        .build();

改变

=> info.accessKey

并插入

=> info.accessToken

连接失败,因为未定义值!

关于javascript - SignalR - 连接启动失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53397014/

相关文章:

c# - 如何在 SignalR 中将参数传递给集线器?

delphi - Delphi 或 C++Builder 中的 SignalR

azure - 显示 Azure 中的用户配额,因为他们不知道如何使用 Portal.azure

c# - 无法加载文件或程序集 'Microsoft.Azure.Storage.Emulator.Controller'

c# - 如何使用 C# 将 AccessKey 值传递到 Azure SignalR 服务器?

javascript - 使用 Javascript 验证 PHP 表单数据

javascript - 等待一组可观察的订阅函数完成

javascript - Jquery Datatables 在 json 表中添加新行

javascript - for in 循环在 javascript 中行为异常

sql - 从 Windows Azure 中的 sFTP 服务器导入数据库