From 1f7513954d6312f3a9a40496d90ae51354e6dc43 Mon Sep 17 00:00:00 2001 From: Alexander Bazo Date: Tue, 27 Aug 2019 10:26:18 +0200 Subject: [PATCH] Add error events --- Readme.md | 1 + lib/com/Events.js | 8 +++++++- lib/com/WebSocketClient.js | 14 +++++++++++++- lib/index.js | 5 +++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 96facf0..a8ab5d1 100644 --- a/Readme.md +++ b/Readme.md @@ -33,6 +33,7 @@ gclient.connect("ws://localhost:8001/gaze"); |---|---|---|---| | ConnectionOpenedEvent | connectionclosed | Invoked when client successfully connected 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 | ## Build diff --git a/lib/com/Events.js b/lib/com/Events.js index b8132a9..680b27f 100644 --- a/lib/com/Events.js +++ b/lib/com/Events.js @@ -25,4 +25,10 @@ class ConnectionClosedEvent extends Event { } } -export {DataEvent, ConnectionOpenedEvent, ConnectionClosedEvent}; \ No newline at end of file +class ConnectionErrorEvent extends Event { + constructor() { + super("erroroccurred"); + } +} + +export {DataEvent, ConnectionOpenedEvent, ConnectionClosedEvent, ConnectionErrorEvent}; \ No newline at end of file diff --git a/lib/com/WebSocketClient.js b/lib/com/WebSocketClient.js index 832403a..e58a60d 100644 --- a/lib/com/WebSocketClient.js +++ b/lib/com/WebSocketClient.js @@ -1,5 +1,5 @@ import Observable from "../utils/Observable.js"; -import { ConnectionOpenedEvent, DataEvent } from "./Events.js"; +import { ConnectionOpenedEvent, ConnectionClosedEvent, ConnectionErrorEvent, DataEvent } from "./Events.js"; class WebSocketClient extends Observable { @@ -10,6 +10,8 @@ class WebSocketClient extends Observable { 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); } @@ -20,6 +22,16 @@ function onOpen() { 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 dataEvent = new DataEvent(event.data); this.notifyAll(dataEvent); diff --git a/lib/index.js b/lib/index.js index 0be8552..20df95a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,6 +12,7 @@ class GazeClient extends Observable { this.client = new WebSocketClient(); this.client.addEventListener("connectionopened", this.onConnected.bind(this)); this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this)); + this.client.addEventListener("erroroccurred", this.onDisconnected.bind(this)); this.client.addEventListener("dataAvailable", this.onDataAvailable .bind(this)); this.client.connect(url); @@ -25,6 +26,10 @@ class GazeClient extends Observable { this.notifyAll(event); } + onError(event) { + this.notifyAll(event); + } + onDataAvailable(event) { this.notifyAll(event); } -- libgit2 0.26.0