SignalR在React Native中发生了奇怪的事情。

huangapple go评论59阅读模式
英文:

SignalR in react-native weird thing happens

问题

这是您提供的代码块的翻译部分:

我们在ReactJS中有一个工作应用程序其中有一个实时聊天部分用于不同用户之间的通信在ReactJS中我们使用@microsoft/signalR一切都运行正常以下是代码块

```javascript
useEffect(() => {
    const ConnectionId = "e48950002479" // user.token;
    const newConnection = new HubConnectionBuilder()
      .withUrl('https://none_of_your_business/chatHub', {
        accessTokenFactory: () => ConnectionId
      }) //替换为您的SignalR hub的URL
      .configureLogging(LogLevel.Debug)
      .build();

    setConnection(newConnection);
  }, [])

  useEffect(() => {
    
    if (connection) {
      connection
        .start()
        .then(() => {
          console.log("SignalR Connected!");

          connection.on("ReceiveMessage", (message) => {
            console.log("Received message:", message);
          });
        })
        .catch((error) => console.log(`SignalR Connection error: ${error}`));
    }
  }, [connection]);

  const sendMessage = async () => {

    await connection.invoke('SendMessage', "1626659a57c6", "Heyo", "1626659a57c6");
  };

然而,当我在React Native中复制相同的代码(使用相同的库)时,它不会发送ConnectionId变量。这真的很奇怪,因为它在ReactJS中运行良好,但在React Native中不起作用。有什么建议吗?


<details>
<summary>英文:</summary>

We have a working application in ReactJS and there is a part which is a real time chat between different users. In reactJs we used @microsoft/signalR and everything works fine. This is the block of code:

useEffect(() => {
const ConnectionId = "e48950002479" // user.token;
const newConnection = new HubConnectionBuilder()
.withUrl('https://none_of_your_business/chatHub', {
accessTokenFactory: () => ConnectionId
}) //replace with the URL of your SignalR hub
.configureLogging(LogLevel.Debug)
.build();

setConnection(newConnection);

}, [])

useEffect(() => {

if (connection) {
  connection
    .start()
    .then(() =&gt; {
      console.log(&quot;SignalR Connected!&quot;);

      connection.on(&quot;ReceiveMessage&quot;, (message) =&gt; {
        console.log(&quot;Received message:&quot;, message);
      });
    })
    .catch((error) =&gt; console.log(`SignalR Connection error: ${error}`));
}

}, [connection]);

const sendMessage = async () => {

await connection.invoke(&#39;SendMessage&#39;, &quot;1626659a57c6&quot;, &quot;Heyo&quot;, &quot;1626659a57c6&quot;);

};


however when I copied the same code for react-native (with the same library), the thing does not send the ConnectionId variable. It is really weird because it works flawless in reactJS but not in react-native. Any tips?

</details>


# 答案1
**得分**: 1

已解决方法是更改:

```javascript
useEffect(() => {
    const ConnectionId = "e48950002479"; // user.token;
    const newConnection = new HubConnectionBuilder()
      .withUrl('https://none_of_your_business/chatHub?access_token=' + ConnectionId) // 替换为您的 SignalR hub 的 URL
      .configureLogging(LogLevel.Debug)
      .build();

    setConnection(newConnection);
  }, [])
英文:

Solved it by changing:

useEffect(() =&gt; {
    const ConnectionId = &quot;e48950002479&quot; // user.token;
    const newConnection = new HubConnectionBuilder()
      .withUrl(&#39;https://none_of_your_business/chatHub?access_token=&#39; + ConnectionId) //replace with the URL of your SignalR hub
      .configureLogging(LogLevel.Debug)
      .build();

    setConnection(newConnection);
  }, [])

huangapple
  • 本文由 发表于 2023年4月4日 13:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定