gazeclient.js 2.76 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

Alexander Bazo committed
50 51 52 53 54 55 56 57 58 59 60 61
  class ConnectionClosedEvent extends Event {
    constructor() {
      super("connectionclosed");
    }
  }

  class ConnectionErrorEvent extends Event {
    constructor() {
      super("erroroccurred");
    }
  }

62 63 64 65 66 67 68 69 70
  class WebSocketClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.ws = new WebSocket(url);
      this.ws.onopen = onOpen.bind(this);
Alexander Bazo committed
71 72
      this.ws.onclose = onClose.bind(this);
      this.ws.onerror = onError.bind(this);
73 74 75 76 77 78
      this.ws.onmessage = onMessage.bind(this);
    }

  }

  function onOpen() {
Alexander Bazo committed
79
    let connectionEvent = new ConnectionOpenedEvent();
80 81 82
    this.notifyAll(connectionEvent);
  }

Alexander Bazo committed
83 84 85 86 87 88 89 90 91 92
  function onClose() {
    let connectionEvent = new ConnectionClosedEvent();
    this.notifyAll(connectionEvent);
  }

  function onError() {
    let connectionEvent = new ConnectionErrorEvent();
    this.notifyAll(connectionEvent);
  }

93
  function onMessage(event) {
Alexander Bazo committed
94 95
    let dataEvent = new DataEvent(event.data);
    this.notifyAll(dataEvent);
96 97 98 99 100 101 102 103 104 105 106
  }

  class GazeClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.url = url;
      this.client = new WebSocketClient();
Alexander Bazo committed
107 108
      this.client.addEventListener("connectionopened", this.onConnected.bind(this));
      this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this));
Alexander Bazo committed
109
      this.client.addEventListener("erroroccurred", this.onDisconnected.bind(this));
Alexander Bazo committed
110
      this.client.addEventListener("dataAvailable", this.onDataAvailable
111 112 113 114
        .bind(this));
      this.client.connect(url);
    }

Alexander Bazo committed
115 116
    onConnected(event) {
      this.notifyAll(event);
117 118
    }

Alexander Bazo committed
119 120
    onDisconnected(event) {
      this.notifyAll(event);
121 122
    }

Alexander Bazo committed
123 124 125 126
    onError(event) {
      this.notifyAll(event);
    }

Alexander Bazo committed
127
    onDataAvailable(event) {
128 129 130 131 132 133
      this.notifyAll(event);
    }

  }

  window.GazeClient = GazeClient;
Alexander Bazo committed
134 135

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