簡體   English   中英

Twilio 可編程聊天 - 從 JS 客戶端斷開連接

[英]Twilio Programmable Chat - Connection dropped from JS client

我有一個 React 應用程序,它使用 Twilio 可編程聊天庫來提供聊天功能。 設置代碼一般是這樣的,都包裹在一個 try/catch 塊中:

  this.accessManager = new AccessManager(twilioToken.token);
  const chatClientOptions = config.debugMode ? { logLevel: 'debug' } : {};
  this.chatClient = await TwilioChat.Client.create(twilioToken.token, chatClientOptions);

  // Regsiter chatClient to get new access tokens from AccessManager
  this.accessManager.on('tokenUpdated', function(am) {
    console.log("Chat client getting new token from AccessManager");
    // get new token from AccessManager and pass it to the library instance
    self.chatClient.updateToken(am.token);
  });

  // IF accesstoken expries, grab a new one from the server
  this.accessManager.on('tokenExpired', async function() {
    console.log("Chat access token expired. Requesting new one from server...");
    // generate new token here and set it to the accessManager
    const updatedToken = (await axios.get(`/users/${user.publicId}/twilioToken`)).data;
    self.accessManager.updateToken(updatedToken.token);
  });

  this.channel = await this.createOrGetChannel();

createOrGetChanne() 獲取通道,然后我們就可以正常獲取消息和發送消息了。

90% 的情況下,一切正常,但看起來有時 Twilsock(我假設這是底層連接管理庫)似乎斷開了我的連接,之后客戶端無法正確調用 this.channel.sendMessage()。 sendMessage() 調用超時,並且沒有要在客戶端上捕獲的 Promise 拒絕(因此我無法重新連接和重試)。

打開調試模式后,我在客戶端上得到的錯誤似乎是:

2018-08-23T22:02:21.425Z Twilsock T: closing socket

那么客戶端不能正常工作。 如果我切換到另一個應用程序或斷開 wifi 然后返回瀏覽器和聊天頁面,我可以在我的手機上重現這種情況(但並非總是如此)。

這是關閉套接字后失敗的消息嘗試的樣子:

2018-08-23T22:02:28.214Z Sync D: POST https://cds.us1.twilio.com/v3/Services/some_id/Lists/some_id/Items ID: RQb01f6b1df35f4f7ead730d990fcc0ef8

2018-08-23T22:02:45.209Z Chat Messages D: Sending text message One more after another app {}

2018-08-23T22:02:45.209Z Chat Session I: Adding command: sendMessage 11707143-0933-429d-bce0-ac2d37b5f699

2018-08-23T22:02:45.210Z Chat Session D: command arguments: {"channelSid":"something","text":"One more after another app","attributes":"{}","action":"sendMessage"} true

2018-08-23T22:02:45.223Z Sync D: POST https://cds.us1.twilio.com/v3/Services/some_id/Lists/some_id/Items ID: RQd8cdea2425724ba8b1c70970c4d890ea

2018-08-23T22:02:48.234Z Twilsock D: request is timed out

緊隨其后的是:

2018-08-23T22:04:04.480Z Chat Session E: Failed to add a command to the session r@https:[some stuff] promiseReactionJob@[native code]

Uncaught (in promise) scheduleAttempt@https://[some stuff] promiseReactionJob@[native code]

刷新頁面(並重新初始化客戶端)可解決此問題。

問題:

1)由於 sendMessage() 承諾沒有被拒絕,有什么我可以做的來捕捉這個並重試或重新初始化連接?

2) 或者,是否有一個回調我可以掛鈎到當 Twilsock 關閉套接字時我可能會重新連接的地方?

3) 是否需要添加任何其他連接管理最佳實踐來使我的應用程序盡可能健壯?

任何幫助表示贊賞。 謝謝,謝謝,謝謝!

Twilio 剛剛發布了他們的聊天 sdk 3.1.0 版,修復了這個連接問題。 升級到此版本應該可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM