2016-12-17 6 views
0

ちょうど開いている接続(例えば、localAddressとlocalPort)についての情報にアクセスするために、既存のtcpソケットの周りにmqtt.Clientをラップする必要がある状況です。これはタイコのようなことをする可能性がありますか?既存のtcpソケットの周りにmqttクライアントをラップするには?

import * as mqtt from 'mqtt'; 
import * as net from 'net'; 
class Client 
{ 
    private _client: mqtt.Client; 
    private _socket: net.Socket; 

    constructor() 
    { 
     this._socket = net.createConnection({host: 'my_host', port: 1234},() => 
     { 
      this._client = new mqtt.Client(this._socket, { 
       // options here. 
      }); 

      // Access the socket info 
      // this._socket.localAddress 
      // this._socket.localPort 
     }); 
    } 
} 

それともmqtt.connect関数で開かれた同じTCPソケットを再利用する別の方法がありますか? ありがとうございました!

私はこのnpm moduleを使用してクライアントを実装しています。

答えて

0

私はそれをmqtt APIを使って調整することができました。 この問題が発生した場合は、クライアントの内部に格納されている埋め込みstreamオブジェクトから必要な情報を取得するだけです。 このようなもの:

import * as mqtt from 'mqtt'; 
class Client 
{ 
    private _client: mqtt.Client; 

    constructor() 
    { 
     this._client = mqtt.connect(`mqtt://${URL_GOES_HERE}`, { 
      // options here. 
     }); 

     this._client.on('connect',() => 
     { 
      let address = this._client.stream.localAddress; 
      let port = this._client.stream.localPort; 
     }); 
    } 
} 
関連する問題