如何订阅刷新令牌事件 [英] How to subscribe to the refresh token event

查看:82
本文介绍了如何订阅刷新令牌事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我需要能够订阅令牌刷新事件,但我不知道如何操作.

The problem is that I need to be able to subscribe to the token refresh event and I can't figure out how.

我知道人们会建议您订阅connectionStatus $并处理ConnectionStatus.ExpiredToken情况,但是在刷新发生时执行永远不会进入这种情况,只有当我尝试使用过期的令牌初始化bot时,执行才会进入这种情况.

I know people advise on subscribing to connectionStatus$ and handling the ConnectionStatus.ExpiredToken case, but the execution never enters that case when refreshing happens, it only enters that case when I try to initialize the bot with an expired token.

令牌刷新事件由库本身每15分钟触发一次,但是没有可观察到的允许我订阅它以获取新刷新的令牌的事件.我现在发现的解决方法是,我设置了15分钟的间隔(通过使用setInterval())来检查连接所使用的令牌是否已更改.

The token refresh event is getting triggered every 15 minutes by the library itself but there is no observable that will allow me to subscribe to it to get the newly refreshed token. The workaround I found for now is that I set an interval of 15 min (by using setInterval()) for checking if the token used by the connection has changed.

有什么主意吗?

代码使用纯净的原始javascript.我正在使用的库代码: https://cdn.botframework.com/botframework-webchat/master/webchat.js

The code is in pure, vanilla, javascript. Library code I'm using: https://cdn.botframework.com/botframework-webchat/master/webchat.js

推荐答案

如果您想捕获令牌,那么我建议您在进行API调用以检索令牌时将其保存.由于您没有提供任何代码,因此我只能猜测您的设置是什么样子.下面是一个简化的示例,演示了如果不存在令牌,则获取令牌(并将其保存到 sessionStorage ).如果令牌确实存在,则每隔25分钟刷新一次(并保存到 sessionStorage ),直到30分钟时,令牌就有失效的危险,并且没有任何活动.

If you are wanting to capture the token, then I would recommend you save it during the API call you make to retrieve it. As you haven't provided any code, I can only guess at what your setup looks like. The below is a simplified example demonstrating getting a token (and saving it to sessionStorage) if no token exists. If a token does exist it's then refreshed every 25 mins (and saved to sessionStorage) as at 30 mins it is in danger of expiring with no activity.

let { token, conversationId } = sessionStorage;

if ( !token ) {
  let res = await fetch( 'http://localhost:3500/directline/token', { method: 'POST' } );

  const { token: directLineToken, conversationId, error } = await res.json();
  sessionStorage[ 'token' ] = directLineToken;
  sessionStorage[ 'conversationId' ] = conversationId;
  token = directLineToken;
}

if (token) {
  await setInterval(async () => {
    let res = await fetch( 'http://localhost:3500/directline/refresh', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify( { token: token } )
    } );
    const { token: directLineToken, conversationId } = await res.json();
    sessionStorage[ 'token' ] = directLineToken;
    sessionStorage[ 'conversationId' ] = conversationId;
    token = directLineToken;
  }, 150000)
}

可以订阅 connectionStatus $ ,但是,这只会显示是否建立了连接,但遇到有关该连接的错误.如果存在令牌问题限制了Web聊天甚至无法连接,那么将永远无法实现可观察的目标.

It is possible to subscribe to connectionStatus$, however that will only show if a connection was made but an error was encountered regarding that connection. If there is a token issue that restricts Web Chat from even connecting, then the observable will never be reached.

const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {

  if(action.payload && action.payload.directLine) {
    const subscription = action.payload.directLine.connectionStatus$.subscribe( {
      error: error => console.log( error ),
      next: value => {
        if ( value === 2 ) {
          console.log('Connected')
        } else if ( value === 4 ) {
          console.log('Encountered a connection error')
        }
      }
    } );
  }
}

希望有帮助!

这篇关于如何订阅刷新令牌事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆