net#SocketConnectOpts TypeScript Examples

The following examples show how to use net#SocketConnectOpts. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: socketManager.ts    From javascript-opensdk with Apache License 2.0 5 votes vote down vote up
/**
   * Opens a connection to the Agent development socket.
   *
   * @param {string} socketAddress The address for the socket
   * @param {number} socketPort The development socket port to connect to
   */
  public openSocket(socketAddress: string, socketPort: number): void {
    // Check if there is already a opened socket
    if (this.socket) {
      logger.debug('OpenSocket(): Socket already exists');
      return;
    }

    // Check if the socket is connected
    if (this.isConnected()) {
      logger.debug('open_socket(): Socket is already connected');
      return;
    }

    // Initialize the new socket
    this.socket = new Socket();

    // Create the socket options object
    const socketOptions: SocketConnectOpts = {
      host: socketAddress,
      port: socketPort,
    };

    this.socket
      .connect(socketOptions, () => {
        if (!this.isConnected()) {
          throw new Error('Failed connecting to Agent socket');
        }

        logger.debug(`Socket connection to ${socketAddress}:${socketPort} established successfully`);
      })
      .on('error', (res) => {
        logger.error(`Socket Connection Failed! message:${res.message}`);
        throw new Error(res.message);
      });
  }