Improve error handling
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 5396795..da75aaf 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -155,6 +155,14 @@
</scode>
</changelog>
</subsection>
+ <subsection name="Web Applications">
+ <changelog>
+ <fix>
+ Examples. Improve the error handling so snakes associated with a user
+ that drops from the network are removed from the game. (markt)
+ </fix>
+ </changelog>
+ </subsection>
<subsection name="Other">
<changelog>
<update>
diff --git a/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java b/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java
index 43d61c6..7f14ab8 100644
--- a/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java
+++ b/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java
@@ -17,7 +17,7 @@
package websocket.snake;
import java.awt.Color;
-import java.io.EOFException;
+import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
@@ -115,19 +115,26 @@
@OnError
- public void onError(Throwable t) throws Throwable {
- // Most likely cause is a user closing their browser. Check to see if
- // the root cause is EOF and if it is ignore it.
- // Protect against infinite loops.
+ public void onError(Throwable t, Session session) throws Throwable {
+ /*
+ * Assume all errors are fatal. Close the session and remove the snake from the game.
+ */
+ session.close();
+ onClose();
+ /*
+ * Correct action depends on root cause. Protect against infinite loops while looking for root cause.
+ */
int count = 0;
Throwable root = t;
while (root.getCause() != null && count < 20) {
root = root.getCause();
count ++;
}
- if (root instanceof EOFException) {
- // Assume this is triggered by the user closing their browser and
- // ignore it.
+ if (root instanceof IOException) {
+ /*
+ * User going away can present in different ways depending on their platform and exactly what went wrong.
+ * Assume that any IO issue is some form of the user going away and ignore it.
+ */
} else {
throw t;
}