local-host 2013-12-11 03:28 采纳率: 100%
浏览 32

AJAX和高速公路

I am displaying a list of items in the web browserthat is initially requested via an AJAX call from the server and then updated by listening to an autobahnjs subscription (which receives change notifications).

Some of my items (a list of job, that sometimes are completed in a few hundred ms, sometimes only after a long time), might be updated at the time the web page is loaded. This is very likely, because the job list is displayed immediatedly after a new job is submitted.

My problem is, that I don't know when the subscription is active, so sometimes changes to my item list occur after I have received the AJAX response but before the subscription becomes active (which results in items never seeing the update).

Starting the subscription ahead of the AJAX call will only partially solve the problem, because now the item list could overwrite a change that occurred earlier via a subscription. Doing so would also require to find out, when the subscription becomes active.

  • 写回答

1条回答 默认 最新

  • 笑故挽风 2013-12-11 09:59
    关注

    With an Autobahn based stack (single server node) running WAMP for both RPC and PubSub, one option is like this:

    var listInitialized = false;
    
    session.subscribe("http://myapp.com/onListChange",
       function (topic, event) {
          if (listInitialized) {
             // update list
          }
       }
    });
    
    session.call("http://myapp.com/getList").then(
       function (res) {
          // initial fill of list
          listInitialized = true;
       }
    );
    

    Since both the subscribe and the call run over a single WAMP connection (and that connection is an ordered WebSocket/TCP transport), the Autobahn server will setup the subscription and deliver events before the call is even processed. That subscription might yield events that arrive before the call returns, but these are ignored via the flag. Later, when the call returns and result has been processed, subsequent events are processed by the client.

    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元