Commit 1f751395 by Alexander Bazo

Add error events

parent 403ba4e4
...@@ -33,6 +33,7 @@ gclient.connect("ws://localhost:8001/gaze"); ...@@ -33,6 +33,7 @@ gclient.connect("ws://localhost:8001/gaze");
|---|---|---|---| |---|---|---|---|
| ConnectionOpenedEvent | connectionclosed | Invoked when client successfully connected to server. | - | | ConnectionOpenedEvent | connectionclosed | Invoked when client successfully connected to server. | - |
| ConnectionClosedEvent | connectionclosed | Invoked when client lost connection to server. | - | | ConnectionClosedEvent | connectionclosed | Invoked when client lost connection to server. | - |
| ConnectionErrorEvent | erroroccurred | Invoked when an error occurred during server client communication. | - |
| DataEvent | dataavailable | Invoked when new gaze data from eye-tracker is available. | On-display (x,y) coordinates for both eyes | | DataEvent | dataavailable | Invoked when new gaze data from eye-tracker is available. | On-display (x,y) coordinates for both eyes |
## Build ## Build
......
...@@ -25,4 +25,10 @@ class ConnectionClosedEvent extends Event { ...@@ -25,4 +25,10 @@ class ConnectionClosedEvent extends Event {
} }
} }
export {DataEvent, ConnectionOpenedEvent, ConnectionClosedEvent}; class ConnectionErrorEvent extends Event {
\ No newline at end of file constructor() {
super("erroroccurred");
}
}
export {DataEvent, ConnectionOpenedEvent, ConnectionClosedEvent, ConnectionErrorEvent};
\ No newline at end of file
import Observable from "../utils/Observable.js"; import Observable from "../utils/Observable.js";
import { ConnectionOpenedEvent, DataEvent } from "./Events.js"; import { ConnectionOpenedEvent, ConnectionClosedEvent, ConnectionErrorEvent, DataEvent } from "./Events.js";
class WebSocketClient extends Observable { class WebSocketClient extends Observable {
...@@ -10,6 +10,8 @@ class WebSocketClient extends Observable { ...@@ -10,6 +10,8 @@ class WebSocketClient extends Observable {
connect(url) { connect(url) {
this.ws = new WebSocket(url); this.ws = new WebSocket(url);
this.ws.onopen = onOpen.bind(this); this.ws.onopen = onOpen.bind(this);
this.ws.onclose = onClose.bind(this);
this.ws.onerror = onError.bind(this);
this.ws.onmessage = onMessage.bind(this); this.ws.onmessage = onMessage.bind(this);
} }
...@@ -20,6 +22,16 @@ function onOpen() { ...@@ -20,6 +22,16 @@ function onOpen() {
this.notifyAll(connectionEvent); this.notifyAll(connectionEvent);
} }
function onClose() {
let connectionEvent = new ConnectionClosedEvent();
this.notifyAll(connectionEvent);
}
function onError() {
let connectionEvent = new ConnectionErrorEvent();
this.notifyAll(connectionEvent);
}
function onMessage(event) { function onMessage(event) {
let dataEvent = new DataEvent(event.data); let dataEvent = new DataEvent(event.data);
this.notifyAll(dataEvent); this.notifyAll(dataEvent);
......
...@@ -12,6 +12,7 @@ class GazeClient extends Observable { ...@@ -12,6 +12,7 @@ class GazeClient extends Observable {
this.client = new WebSocketClient(); this.client = new WebSocketClient();
this.client.addEventListener("connectionopened", this.onConnected.bind(this)); this.client.addEventListener("connectionopened", this.onConnected.bind(this));
this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this)); this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this));
this.client.addEventListener("erroroccurred", this.onDisconnected.bind(this));
this.client.addEventListener("dataAvailable", this.onDataAvailable this.client.addEventListener("dataAvailable", this.onDataAvailable
.bind(this)); .bind(this));
this.client.connect(url); this.client.connect(url);
...@@ -25,6 +26,10 @@ class GazeClient extends Observable { ...@@ -25,6 +26,10 @@ class GazeClient extends Observable {
this.notifyAll(event); this.notifyAll(event);
} }
onError(event) {
this.notifyAll(event);
}
onDataAvailable(event) { onDataAvailable(event) {
this.notifyAll(event); this.notifyAll(event);
} }
......
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