gazeclient.js 2.09 KB
Newer Older
Alexander Bazo committed
1
// GazeClient Version 1.0 - [https://lab.las3.de/gitlab/eye-tracking-classroom/gaze-client.js]
Alexander Bazo committed
2
(function () {
3
  'use strict';
Alexander Bazo committed
4

5
  class Observable {
Alexander Bazo committed
6

7 8 9
    constructor() {
      this.listeners = {};
    }
Alexander Bazo committed
10

11 12 13 14 15 16
    addEventListener(type, callback) {
      if (this.listeners[type] === undefined) {
        this.listeners[type] = [];
      }
      this.listeners[type].push(callback);
    }
Alexander Bazo committed
17

18 19 20 21 22 23 24 25
    notifyAll(event) {
      let listeners = this.listeners[event.type];
      if (listeners) {
        for (let i = 0; i < listeners.length; i++) {
          listeners[i](event);
        }
      }
    }
Alexander Bazo committed
26

27
  }
Alexander Bazo committed
28

Alexander Bazo committed
29 30 31 32 33 34 35 36
  class Event {

    constructor(type, data) {
      this.type = type;
      this.data = data;
    }

  }
Alexander Bazo committed
37

Alexander Bazo committed
38
  class DataEvent extends Event {
39
    constructor(data) {
Alexander Bazo committed
40
      super("dataavailable", data);
41 42
    }
  }
Alexander Bazo committed
43

Alexander Bazo committed
44
  class ConnectionOpenedEvent extends Event {
45
    constructor() {
Alexander Bazo committed
46
      super("connectionopened");
47 48
    }
  }
Alexander Bazo committed
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  class WebSocketClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.ws = new WebSocket(url);
      this.ws.onopen = onOpen.bind(this);
      this.ws.onmessage = onMessage.bind(this);
    }

  }

  function onOpen() {
Alexander Bazo committed
65
    let connectionEvent = new ConnectionOpenedEvent();
66 67 68 69
    this.notifyAll(connectionEvent);
  }

  function onMessage(event) {
Alexander Bazo committed
70 71
    let dataEvent = new DataEvent(event.data);
    this.notifyAll(dataEvent);
72 73 74 75 76 77 78 79 80 81 82
  }

  class GazeClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.url = url;
      this.client = new WebSocketClient();
Alexander Bazo committed
83 84 85
      this.client.addEventListener("connectionopened", this.onConnected.bind(this));
      this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this));
      this.client.addEventListener("dataAvailable", this.onDataAvailable
86 87 88 89
        .bind(this));
      this.client.connect(url);
    }

Alexander Bazo committed
90 91
    onConnected(event) {
      this.notifyAll(event);
92 93
    }

Alexander Bazo committed
94 95
    onDisconnected(event) {
      this.notifyAll(event);
96 97
    }

Alexander Bazo committed
98
    onDataAvailable(event) {
99 100 101 102 103 104
      this.notifyAll(event);
    }

  }

  window.GazeClient = GazeClient;
Alexander Bazo committed
105 106

}());
Alexander Bazo committed
107
//# sourceMappingURL=gazeclient.js.map