WebChat开启sendTypingIndicator时如何配置typing Activity的发送间隔?
WebChat 本身没有直接暴露自定义 typing 发送间隔的公开配置参数,但可以通过改写默认发送逻辑,自行控制发送频率,具体实现方案如下:
实现方法
先关闭默认的 sendTypingIndicator 逻辑,通过自定义中间件监听输入事件,自己加节流逻辑控制typing事件的发送间隔。
完整示例代码
// 自定义typing发送间隔,单位为毫秒,这里示例是10秒 const CUSTOM_TYPING_INTERVAL = 10000; // 记录上次发送typing的时间 let lastTypingSendTime = 0; window.WebChat.renderWebChat( { directLine: window.WebChat.createDirectLine({ token }), // 关闭默认的自动发送typing逻辑 sendTypingIndicator: false, store, // 新增自定义中间件监听输入事件 createMiddleware: () => next => action => { // 匹配用户输入框内容变更的动作 if (action.type === 'WEB_CHAT/SET_SEND_BOX') { const currentTime = Date.now(); // 只有距离上次发送typing的时间超过自定义间隔时才触发新的发送 if (currentTime - lastTypingSendTime >= CUSTOM_TYPING_INTERVAL) { store.dispatch({ type: 'WEB_CHAT/SEND_TYPING' }); lastTypingSendTime = currentTime; } } return next(action); } }, document.getElementById('webchat') );
如果你需要在用户停止输入后立刻停止typing状态上报,可以额外加一个定时器,监听到用户超过指定时长没有输入时,就重置lastTypingSendTime即可。
内容的提问来源于stack exchange,提问作者naina
相关产品推荐
相关产品推荐

