【问题标题】:How to identify the protocol of the Meteor connection如何识别 Meteor 连接的协议
【发布时间】:2017-05-19 10:01:12
【问题描述】:

我想知道如果当前协议是使用 websocket 还是轮询,如何识别它。

--在客户端。 (为确定性而附加)

我从调试控制台中找到了有效信息。

Meteor.connection._stream.socket.protocol

它似乎只有一个价值......

['websocket',
'xdr-streaming',
'xhr-streaming',
'iframe-eventsource',
'iframe-htmlfile',
'xdr-polling',
'xhr-polling',
'iframe-xhr-polling',
'jsonp-polling'];

是否有更优雅的方式来识别当前协议? 什么时候是检测协议的最快时机?

顺便说一句,当需要粘性会话时,我需要它使用不同的 DDP 服务器,因为 AWS ELB 不支持粘性会话和 websocket。

【问题讨论】:

    标签: meteor protocols ddp


    【解决方案1】:

    Meteor 使用 DDP 协议。要连接到不同的 Meteor 服务器并调用其方法,请使用 DDP.connect,如下所示。

    import { DDP } from 'meteor/ddp-client'
    DDP.connect(url)
    

    很遗憾,没有优雅的获取协议。 onConnection 返回一个包含一些信息的对象。

    Meteor.onConnection(obj => 
      { console.log(obj.httpHeaders.x-forwarded-proto); });
    

    这将为 websocket 返回“ws”。这种获取协议的方式并不优雅!

    【讨论】:

    • 也许我的问题是错误的。我想知道 SockJS 连接的协议。其中 ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'];
    【解决方案2】:

    Meteor.status() 提供反应式数据源。 (https://docs.meteor.com/api/connections.html#DDP-connect)

    if (Meteor.isClient) {
      Tracker.autorun(() => {
        const stat = Meteor.status();
        if (stat.status === 'connected') {
          console.log(Meteor.connection._stream.socket.protocol);
        }
      });
    }
    

    类似的东西会在客户端提供当前协议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2021-06-03
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多