github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/app/services/sockets.js (about)

     1  import Service from '@ember/service';
     2  import config from 'nomad-ui/config/environment';
     3  import { getOwner } from '@ember/application';
     4  
     5  export default Service.extend({
     6    getTaskStateSocket(taskState, command) {
     7      const mirageEnabled =
     8        config.environment !== 'production' &&
     9        config['ember-cli-mirage'] &&
    10        config['ember-cli-mirage'].enabled !== false;
    11  
    12      if (mirageEnabled) {
    13        return new Object({
    14          messageDisplayed: false,
    15  
    16          send(e) {
    17            if (!this.messageDisplayed) {
    18              this.messageDisplayed = true;
    19              this.onmessage({ data: `{"stdout":{"data":"${btoa('unsupported in Mirage\n\r')}"}}` });
    20            } else {
    21              this.onmessage({ data: e.replace('stdin', 'stdout') });
    22            }
    23          },
    24        });
    25      } else {
    26        const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
    27        const applicationAdapter = getOwner(this).lookup('adapter:application');
    28        const prefix = `${applicationAdapter.host ||
    29          window.location.host}/${applicationAdapter.urlPrefix()}`;
    30  
    31        return new WebSocket(
    32          `${protocol}//${prefix}/client/allocation/${taskState.allocation.id}` +
    33            `/exec?task=${taskState.name}&tty=true&ws_handshake=true` +
    34            `&command=${encodeURIComponent(`["${command}"]`)}`
    35        );
    36      }
    37    },
    38  });