Author: markt Date: Wed Oct 2 08:40:39 2013 New Revision: 1528370 URL: http://svn.apache.org/r1528370 Log: Handle error when user closes browser while playing snake game.
Modified: tomcat/tc7.0.x/trunk/ (props changed) tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml tomcat/tc7.0.x/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java Propchange: tomcat/tc7.0.x/trunk/ ------------------------------------------------------------------------------ Merged /tomcat/trunk:r1528369 Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1528370&r1=1528369&r2=1528370&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Wed Oct 2 08:40:39 2013 @@ -87,6 +87,10 @@ <code>org.apache.catalina.ha.session.JvmRouteBinderValve</code>. (kfujino) </add> + <fix> + Handle the case when a user closes the browser whilst playing the + snake game in the JSR356 WebSocket examples. (markt) + </fix> </changelog> </subsection> </section> Modified: tomcat/tc7.0.x/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java?rev=1528370&r1=1528369&r2=1528370&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java (original) +++ tomcat/tc7.0.x/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java Wed Oct 2 08:40:39 2013 @@ -17,11 +17,13 @@ package websocket.snake; import java.awt.Color; +import java.io.EOFException; import java.util.Iterator; import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; import javax.websocket.OnClose; +import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; @@ -110,4 +112,24 @@ public class SnakeAnnotation { SnakeTimer.broadcast(String.format("{'type': 'leave', 'id': %d}", Integer.valueOf(id))); } + + + @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. + 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. + } else { + throw t; + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org