Author: markt
Date: Wed Nov 20 13:52:31 2013
New Revision: 1543816
URL: http://svn.apache.org/r1543816
Log:
Implement the restriction required by the JSR356 specification that only one
message can be written to a remote endpoint at a time.
Modified:
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java
Modified:
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?rev=1543816&r1=1543815&r2=1543816&view=diff
==============================================================================
---
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java
(original)
+++
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java
Wed Nov 20 13:52:31 2013
@@ -22,16 +22,22 @@ import java.util.concurrent.CopyOnWriteA
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;
import javax.websocket.server.ServerEndpoint;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
import util.HTMLFilter;
@ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {
+ private static final Log log = LogFactory.getLog(ChatAnnotation.class);
+
private static final String GUEST_PREFIX = "Guest";
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private static final Set<ChatAnnotation> connections =
@@ -72,11 +78,22 @@ public class ChatAnnotation {
}
+
+
+ @OnError
+ public void onError(Throwable t) throws Throwable {
+ log.error("Chat Error: " + t.toString(), t);
+ }
+
+
private static void broadcast(String msg) {
for (ChatAnnotation client : connections) {
try {
- client.session.getBasicRemote().sendText(msg);
+ synchronized (client) {
+ client.session.getBasicRemote().sendText(msg);
+ }
} catch (IOException e) {
+ log.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]