Making Jetty WebSocket API Echo Examples more consistent (for doc)

This commit is contained in:
Joakim Erdfelt 2013-05-06 13:47:20 -07:00
parent 143b387727
commit f2528642d9
3 changed files with 19 additions and 29 deletions

View File

@ -20,8 +20,6 @@ package examples.echo;
import java.io.IOException;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
/**
@ -29,26 +27,21 @@ import org.eclipse.jetty.websocket.api.WebSocketAdapter;
*/
public class AdapterEchoSocket extends WebSocketAdapter
{
private static final Logger LOG = Log.getLogger(AdapterEchoSocket.class);
@Override
public void onWebSocketText(String message)
{
if (isNotConnected())
if (isConnected())
{
LOG.debug("WebSocket Not Connected");
return;
}
try
{
LOG.debug("Echoing back message [{}]",message);
// echo the data back
getRemote().sendString(message);
}
catch (IOException e)
{
e.printStackTrace();
try
{
System.out.printf("Echoing back message [%s]%n",message);
// echo the message back
getRemote().sendString(message);
}
catch (IOException e)
{
e.printStackTrace(System.err);
}
}
}
}

View File

@ -33,8 +33,9 @@ public class AnnotatedEchoSocket
{
if (session.isOpen())
{
return;
System.out.printf("Echoing back message [%s]%n",message);
// echo the message back
session.getRemote().sendStringByFuture(message);
}
session.getRemote().sendStringByFuture(message);
}
}

View File

@ -18,9 +18,6 @@
package examples.echo;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketListener;
@ -29,7 +26,6 @@ import org.eclipse.jetty.websocket.api.WebSocketListener;
*/
public class ListenerEchoSocket implements WebSocketListener
{
private static final Logger LOG = Logger.getLogger(ListenerEchoSocket.class.getName());
private Session outbound;
@Override
@ -53,17 +49,17 @@ public class ListenerEchoSocket implements WebSocketListener
@Override
public void onWebSocketError(Throwable cause)
{
LOG.log(Level.WARNING,"onWebSocketError",cause);
cause.printStackTrace(System.err);
}
@Override
public void onWebSocketText(String message)
{
if (outbound == null)
if ((outbound != null) && (outbound.isOpen()))
{
return;
System.out.printf("Echoing back message [%s]%n",message);
// echo the message back
outbound.getRemote().sendStringByFuture(message);
}
outbound.getRemote().sendStringByFuture(message);
}
}