diff --git a/lib/com/Events.js b/lib/com/Events.js new file mode 100644 index 0000000..b8132a9 --- /dev/null +++ b/lib/com/Events.js @@ -0,0 +1,28 @@ +class Event { + + constructor(type, data) { + this.type = type; + this.data = data; + } + +} + +class DataEvent extends Event { + constructor(data) { + super("dataavailable", data); + } +} + +class ConnectionOpenedEvent extends Event { + constructor() { + super("connectionopened"); + } +} + +class ConnectionClosedEvent extends Event { + constructor() { + super("connectionclosed"); + } +} + +export {DataEvent, ConnectionOpenedEvent, ConnectionClosedEvent}; \ No newline at end of file diff --git a/lib/com/WebSocketClient.js b/lib/com/WebSocketClient.js new file mode 100644 index 0000000..832403a --- /dev/null +++ b/lib/com/WebSocketClient.js @@ -0,0 +1,28 @@ +import Observable from "../utils/Observable.js"; +import { ConnectionOpenedEvent, DataEvent } from "./Events.js"; + +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() { + let connectionEvent = new ConnectionOpenedEvent(); + this.notifyAll(connectionEvent); +} + +function onMessage(event) { + let dataEvent = new DataEvent(event.data); + this.notifyAll(dataEvent); +} + +export default WebSocketClient; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 59c5432..0be8552 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,5 @@ -/* eslint-env browser */ - -import { Event, Observable } from "./Observable.js"; -import WebSocketClient from "./WebSocketClient.js"; +import Observable from "./utils/Observable.js"; +import WebSocketClient from "./com/WebSocketClient.js"; class GazeClient extends Observable { @@ -12,21 +10,22 @@ class GazeClient extends Observable { connect(url) { this.url = url; this.client = new WebSocketClient(); - this.client.addEventListener("connected", this.onConnected.bind(this)); - this.client.addEventListener("gazeDataAvailable", this.onGazeDataAvailable + this.client.addEventListener("connectionopened", this.onConnected.bind(this)); + this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this)); + this.client.addEventListener("dataAvailable", this.onDataAvailable .bind(this)); this.client.connect(url); } - onOpen() { - this.client.send("hello server"); + onConnected(event) { + this.notifyAll(event); } - onConnected(event) { - // TODO: Implement event broadcasting + onDisconnected(event) { + this.notifyAll(event); } - onGazeDataAvailable(event) { + onDataAvailable(event) { this.notifyAll(event); } diff --git a/lib/utils/Observable.js b/lib/utils/Observable.js new file mode 100644 index 0000000..b3f1939 --- /dev/null +++ b/lib/utils/Observable.js @@ -0,0 +1,25 @@ +class Observable { + + constructor() { + this.listeners = {}; + } + + addEventListener(type, callback) { + if (this.listeners[type] === undefined) { + this.listeners[type] = []; + } + this.listeners[type].push(callback); + } + + notifyAll(event) { + let listeners = this.listeners[event.type]; + if (listeners) { + for (let i = 0; i < listeners.length; i++) { + listeners[i](event); + } + } + } + +} + +export default Observable; \ No newline at end of file