WebSocketClient.js 741 Bytes
Newer Older
Alexander Bazo committed
1 2
/* eslint-env browser */

3 4 5 6 7 8
import { Observable, Event } from "./Observable.js";

class GazeDataEvent extends Event {
  constructor(data) {
    super("gazeDataAvailable", data);
  }
Alexander Bazo committed
9 10
}

11 12 13 14
class ConnectionEvent extends Event {
  constructor() {
    super("connected");
  }
Alexander Bazo committed
15 16
}

17 18 19 20 21 22 23
class WebSocketClient extends Observable {

  constructor() {
    super();
  }

  connect(url) {
Alexander Bazo committed
24

25 26 27 28
    this.ws = new WebSocket(url);
    this.ws.onopen = onOpen.bind(this);
    this.ws.onmessage = onMessage.bind(this);
  }
Alexander Bazo committed
29

30 31 32 33 34 35
}

function onOpen() {
  let connectionEvent = new ConnectionEvent();
  this.notifyAll(connectionEvent);
}
Alexander Bazo committed
36

37 38 39
function onMessage(event) {
  let gazeEvent = new GazeDataEvent(event.data);
  this.notifyAll(gazeEvent);
Alexander Bazo committed
40 41 42
}

export default WebSocketClient;