简体   繁体   English

同步 XMLHttpRequest Strophe.js

[英]Synchronous XMLHttpRequest Strophe.js

I am using Strophe.js (JS based Xmpp library).我正在使用 Strophe.js(基于 JS 的 Xmpp 库)。 The problem is that I need to establish synchronous connections so that the responses I receive are in the correct order.问题是我需要建立同步连接,以便我收到的响应顺序正确。 But when I use synchronous connections, the page sort of becomes stuck.但是当我使用同步连接时,页面有点卡住了。 Even a right-click to open the console takes minutes.即使是右键单击打开控制台也需要几分钟。

I get this warning on page:我在页面上收到此警告:

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. [弃用] 主线程上的同步 XMLHttpRequest 已弃用,因为它对最终用户的体验有不利影响。 For more help, check https://xhr.spec.whatwg.org/ .如需更多帮助,请查看https://xhr.spec.whatwg.org/

Please help!请帮忙!

The warning just tells you that it is greatly preferred to use async calls, for a reason.该警告只是告诉您出于某种原因,最好使用异步调用。

A synchronous call will block the rest of your code until a response has been received.同步调用将阻塞代码的 rest,直到收到响应。 Somehow, it takes a long time for your synchronous call to return.不知何故,您的同步调用需要很长时间才能返回。

Without looking at your code it is hard to tell why, but you could try retrieving the resource you are trying to get by typing its URL in in a browser and see how long it takes to get a response, or if you get a response at all, and if you see any errors.如果不查看您的代码,很难说出原因,但您可以尝试通过在浏览器中键入其 URL 来检索您尝试获取的资源,并查看获得响应需要多长时间,或者您是否在全部,如果您看到任何错误。

If the resource can't be retrieved or is slow in coming, you know where the problem is and where to fix it.如果无法检索到资源或资源获取速度缓慢,您就会知道问题出在哪里以及解决问题的位置。

There are some ways of getting XMLHttpRequest results in the right order:有一些方法可以按正确的顺序获得XMLHttpRequest结果:

  • Use synchronous calls and try to make sure the server responds fast enough;使用同步调用并尽量确保服务器响应速度足够快;
  • Use asynchronous calls and start the next call when the previous has been returned succesfully (so start the next request in the callback of the previous one);使用异步调用,当上一个调用成功返回时开始下一个调用(所以在上一个请求的回调中开始下一个请求);
  • Use asynchronous calls, store a request number, store the responses in a temporary array until all the calls have returned, and sort them;使用异步调用,存储请求编号,将响应存储在临时数组中,直到所有调用都返回,并对它们进行排序;
  • Some hip and modern way of doing it I don't know about but Google probably does.一些我不知道的时髦和现代的方式,但谷歌可能知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM