import Observable from "../utils/Observable.js"; import { ConnectionOpenedEvent, ConnectionClosedEvent, ConnectionErrorEvent, DataEvent } from "./Events.js"; import GazeData from "../data/GazeData.js"; class WebSocketClient extends Observable { constructor() { super(); } connect(url) { this.ws = new WebSocket(url); this.ws.onopen = onOpen.bind(this); this.ws.onclose = onClose.bind(this); this.ws.onerror = onError.bind(this); this.ws.onmessage = onMessage.bind(this); } } function onOpen() { let connectionEvent = new ConnectionOpenedEvent(); this.notifyAll(connectionEvent); } function onClose() { let connectionEvent = new ConnectionClosedEvent(); this.notifyAll(connectionEvent); } function onError() { let connectionEvent = new ConnectionErrorEvent(); this.notifyAll(connectionEvent); } function onMessage(event) { let data = GazeData.fromDataString(event.data), dataEvent = new DataEvent(data); this.notifyAll(dataEvent); } export default WebSocketClient;