Commit c843b11c by Alexander Bazo

Update demo

parent 1871d8b6
(function () {
'use strict';
/* eslint-env browser */
class Event {
constructor(type, data) {
this.type = type;
this.data = data;
}
}
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);
}
}
}
}
/* eslint-env browser */
class GazeDataEvent extends Event {
constructor(data) {
super("gazeDataAvailable", data);
}
}
class ConnectionEvent extends Event {
constructor() {
super("connected");
}
}
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 ConnectionEvent();
this.notifyAll(connectionEvent);
}
function onMessage(event) {
let gazeEvent = new GazeDataEvent(event.data);
this.notifyAll(gazeEvent);
}
/* eslint-env browser */
class GazeClient extends Observable {
constructor() {
super();
}
connect(url) {
this.url = url;
this.client = new WebSocketClient();
this.client.addEventListener("connected", this.onConnected.bind(this));
this.client.addEventListener("gazeDataAvailable", this.onGazeDataAvailable
.bind(this));
this.client.connect(url);
}
onOpen() {
this.client.send("hello server");
}
onConnected(event) {
// TODO: Implement event broadcasting
}
onGazeDataAvailable(event) {
this.notifyAll(event);
}
}
window.GazeClient = GazeClient;
}());
(function(){"use strict";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)}}}}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 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)}class GazeClient extends Observable{constructor(){super()}connect(url){this.url=url;this.client=new WebSocketClient;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)}onConnected(event){this.notifyAll(event)}onDisconnected(event){this.notifyAll(event)}onDataAvailable(event){this.notifyAll(event)}}window.GazeClient=GazeClient})();
\ No newline at end of file
......@@ -13,7 +13,7 @@
</header>
<content>
</content>
<script type="application/javascript" src="gazeclient.js"></script>
<script type="application/javascript" src="gazeclient.min.js"></script>
<script type="module" src="resources/js/index.js"></script>
</body>
......
......@@ -3,8 +3,18 @@
var gclient = new GazeClient();
gclient.connect("ws://localhost:8001/gaze");
gclient.addEventListener("gazeDataAvailable", onGazeDataAvailable);
gclient.addEventListener("connectionopened", onConnected);
gclient.addEventListener("dataAvailable", onGazeDataAvailable);
gclient.addEventListener("connectionclosed", onDisconnected);
function onConnected(event) {
console.log(event);
}
function onGazeDataAvailable(event) {
console.log(event);
}
function onDisconnected(event) {
console.log(event);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment