399669 - Remove WebSocketConnection in favor of websocket.api.Session

This commit is contained in:
Joakim Erdfelt 2013-01-31 12:09:04 -07:00
parent 1aa6e63d25
commit 1480f3d8fc
20 changed files with 94 additions and 353 deletions

View File

@ -1,133 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.api;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;
/**
* Connection interface for WebSocket protocol <a href="https://tools.ietf.org/html/rfc6455">RFC-6455</a>.
*
* @deprecated replaced with more JSR friendly {@link Session} interface
*/
@Deprecated
public interface WebSocketConnection
{
/**
* Send a websocket Close frame, without a status code or reason.
* <p>
* Basic usage: results in an non-blocking async write, then connection close.
*
* @see StatusCode
* @see #close(int, String)
*/
public void close() throws IOException;
/**
* Send a websocket Close frame, with status code.
* <p>
* Advanced usage: results in an non-blocking async write, then connection close.
*
* @param statusCode
* the status code
* @param reason
* the (optional) reason. (can be null for no reason)
* @see StatusCode
*/
public void close(int statusCode, String reason) throws IOException;
/**
* Get the address of the local side.
*
* @return the local side address
*/
public InetSocketAddress getLocalAddress();
/**
* Access the (now read-only) {@link WebSocketPolicy} in use for this connection.
*
* @return the policy in use
*/
WebSocketPolicy getPolicy();
/**
* Get the address of the remote side.
*
* @return the remote side address
*/
public InetSocketAddress getRemoteAddress();
/**
* Get the Request URI
*
* @return the requested URI
*/
public URI getRequestURI();
/**
* Get the SubProtocol in use for this connection.
*
* @return the negotiated sub protocol name in use for this connection, can be null if there is no sub-protocol negotiated.
*/
String getSubProtocol();
/**
* Is the connection open.
*
* @return true if open
*/
public boolean isOpen();
/**
* Send a single ping messages.
* <p>
* NIO style with callbacks, allows for knowledge of successful ping send.
* <p>
* Use @OnWebSocketFrame and monitor Pong frames
*
* @param the
* ping application data
*/
void ping(ByteBuffer buf) throws IOException;
/**
* Suspend a the incoming read events on the connection.
*
* @return the suspend token suitable for resuming the reading of data on the connection.
*/
SuspendToken suspend();
/**
* Send an async binary message.
*/
Future<Void> write(byte buf[], int offset, int len);
/**
* Send an async binary message.
*/
Future<Void> write(ByteBuffer buffer);
/**
* Send an async text messages.
*/
Future<Void> write(String message);
}

View File

@ -21,15 +21,15 @@ package org.eclipse.jetty.websocket.api.io;
import java.io.IOException;
import java.io.OutputStream;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
public class WebSocketOutputStream extends OutputStream
{
private final WebSocketConnection conn;
private final Session session;
public WebSocketOutputStream(WebSocketConnection conn)
public WebSocketOutputStream(Session session)
{
this.conn = conn;
this.session = session;
}
@Override

View File

@ -22,16 +22,16 @@ import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
public class WebSocketWriter extends Writer
{
private final Charset charset = Charset.forName("UTF-8");
private final WebSocketConnection conn;
private final Session session;
public WebSocketWriter(WebSocketConnection conn)
public WebSocketWriter(Session session)
{
this.conn = conn;
this.session = session;
}
@Override

View File

@ -23,8 +23,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
@ -42,7 +42,7 @@ public class SimpleEchoClient
{
private final CountDownLatch closeLatch;
@SuppressWarnings("unused")
private WebSocketConnection conn;
private Session session;
public SimpleEchoSocket()
{
@ -58,25 +58,25 @@ public class SimpleEchoClient
public void onClose(int statusCode, String reason)
{
System.out.printf("Connection closed: %d - %s%n",statusCode,reason);
this.conn = null;
this.session = null;
this.closeLatch.countDown(); // trigger latch
}
@OnWebSocketConnect
public void onConnect(WebSocketConnection conn)
public void onConnect(Session session)
{
System.out.printf("Got connect: %s%n",conn);
this.conn = conn;
System.out.printf("Got connect: %s%n",session);
this.session = session;
try
{
Future<Void> fut;
fut = conn.write("Hello");
fut = session.getRemote().sendStringByFuture("Hello");
fut.get(2,TimeUnit.SECONDS); // wait for send to complete.
fut = conn.write("Thanks for the conversation.");
fut = session.getRemote().sendStringByFuture("Thanks for the conversation.");
fut.get(2,TimeUnit.SECONDS); // wait for send to complete.
conn.close(StatusCode.NORMAL,"I'm done");
session.close(StatusCode.NORMAL,"I'm done");
}
catch (Throwable t)
{

View File

@ -21,11 +21,9 @@ package org.eclipse.jetty.websocket.common;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.StringUtil;
@ -43,7 +41,6 @@ import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.SuspendToken;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
@ -53,7 +50,7 @@ import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
import org.eclipse.jetty.websocket.common.events.EventDriver;
@ManagedObject
public class WebSocketSession extends ContainerLifeCycle implements Session, WebSocketConnection, IncomingFrames
public class WebSocketSession extends ContainerLifeCycle implements Session, IncomingFrames
{
private static final Logger LOG = Log.getLogger(WebSocketSession.class);
private final URI requestURI;
@ -228,18 +225,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web
return remote.getInetSocketAddress();
}
@Override
public URI getRequestURI()
{
return requestURI;
}
@Override
public String getSubProtocol()
{
return upgradeResponse.getAcceptedSubProtocol();
}
@Override
public UpgradeRequest getUpgradeRequest()
{
@ -290,7 +275,14 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web
@Override
public boolean isSecure()
{
return getRequestURI().getScheme().equalsIgnoreCase("wss");
if (upgradeRequest == null)
{
throw new IllegalStateException("No valid UpgradeRequest yet");
}
URI requestURI = upgradeRequest.getRequestURI();
return "wss".equalsIgnoreCase(requestURI.getScheme());
}
/**
@ -320,12 +312,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web
}
}
@Override
public void ping(ByteBuffer buf) throws IOException
{
remote.sendPing(buf);
}
public void setActive(boolean active)
{
this.active = active;
@ -391,22 +377,4 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web
builder.append("]");
return builder.toString();
}
@Override
public Future<Void> write(byte[] buf, int offset, int len)
{
return remote.sendBytesByFuture(ByteBuffer.wrap(buf,offset,len));
}
@Override
public Future<Void> write(ByteBuffer buffer)
{
return remote.sendBytesByFuture(buffer);
}
@Override
public Future<Void> write(String message)
{
return remote.sendStringByFuture(message);
}
}

View File

@ -18,19 +18,14 @@
package org.eclipse.jetty.websocket.common.extensions.mux;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.SuspendToken;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.WriteCallback;
@ -47,7 +42,7 @@ import org.eclipse.jetty.websocket.common.io.IOState;
/**
* MuxChannel, acts as WebSocketConnection for specific sub-channel.
*/
public class MuxChannel implements WebSocketConnection, LogicalConnection, IncomingFrames, SuspendToken
public class MuxChannel implements LogicalConnection, IncomingFrames, SuspendToken
{
private static final Logger LOG = Log.getLogger(MuxChannel.class);
@ -128,25 +123,12 @@ public class MuxChannel implements WebSocketConnection, LogicalConnection, Incom
return muxer.getRemoteAddress();
}
@Override
public URI getRequestURI()
{
// TODO Auto-generated method stub
return null;
}
@Override
public WebSocketSession getSession()
{
return session;
}
@Override
public String getSubProtocol()
{
return this.subProtocol;
}
/**
* Incoming exceptions from Muxer.
*/
@ -214,15 +196,6 @@ public class MuxChannel implements WebSocketConnection, LogicalConnection, Incom
muxer.output(channelId,frame,callback);
}
/**
* Ping frame destined for the Muxer
*/
@Override
public void ping(ByteBuffer buf) throws IOException
{
outgoingFrame(WebSocketFrame.ping().setPayload(buf),null);
}
@Override
public void resume()
{
@ -257,37 +230,4 @@ public class MuxChannel implements WebSocketConnection, LogicalConnection, Incom
// TODO: how to suspend reading?
return this;
}
/**
* Generate a binary message, destined for Muxer
*/
@Override
public Future<Void> write(byte[] buf, int offset, int len)
{
ByteBuffer bb = ByteBuffer.wrap(buf,offset,len);
return write(bb);
}
/**
* Generate a binary message, destined for Muxer
*/
@Override
public Future<Void> write(ByteBuffer buffer)
{
if (LOG.isDebugEnabled())
{
LOG.debug("write with {}",BufferUtil.toDetailString(buffer));
}
WebSocketFrame frame = WebSocketFrame.binary().setPayload(buffer);
return outgoingAsyncFrame(frame);
}
/**
* Generate a text message, destined for Muxer
*/
@Override
public Future<Void> write(String message)
{
return outgoingAsyncFrame(WebSocketFrame.text(message));
}
}

View File

@ -40,7 +40,6 @@ import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.websocket.api.CloseException;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.SuspendToken;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.WriteCallback;

View File

@ -18,7 +18,7 @@
package examples.echo;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@ -29,12 +29,12 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
public class AnnotatedEchoSocket
{
@OnWebSocketMessage
public void onText(WebSocketConnection conn, String message)
public void onText(Session session, String message)
{
if (conn.isOpen())
if (session.isOpen())
{
return;
}
conn.write(message);
session.getRemote().sendStringByFuture(message);
}
}

View File

@ -18,7 +18,7 @@
package org.eclipse.jetty.websocket.common.annotations;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@ -32,7 +32,7 @@ public class BadBinarySignatureSocket
* Declaring a non-void return type
*/
@OnWebSocketMessage
public boolean onBinary(WebSocketConnection conn, byte buf[], int offset, int len)
public boolean onBinary(Session session, byte buf[], int offset, int len)
{
return false;
}

View File

@ -18,7 +18,7 @@
package org.eclipse.jetty.websocket.common.annotations;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@ -32,7 +32,7 @@ public class BadTextSignatureSocket
* Declaring a static method
*/
@OnWebSocketMessage
public static void onText(WebSocketConnection conn, String text)
public static void onText(Session session, String text)
{
/* do nothing */
}

View File

@ -18,7 +18,7 @@
package org.eclipse.jetty.websocket.common.annotations;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
/**
@ -29,7 +29,7 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
public class NotASocket
{
@OnWebSocketConnect
public void onConnect(WebSocketConnection conn)
public void onConnect(Session session)
{
/* do nothing */
}

View File

@ -18,14 +18,9 @@
package org.eclipse.jetty.websocket.common.io;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;
import org.eclipse.jetty.websocket.api.SuspendToken;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.WriteCallback;
@ -35,7 +30,7 @@ import org.eclipse.jetty.websocket.common.LogicalConnection;
import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.junit.rules.TestName;
public class LocalWebSocketConnection implements WebSocketConnection, LogicalConnection, IncomingFrames
public class LocalWebSocketConnection implements LogicalConnection, IncomingFrames
{
private final String id;
private WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
@ -105,24 +100,12 @@ public class LocalWebSocketConnection implements WebSocketConnection, LogicalCon
return null;
}
@Override
public URI getRequestURI()
{
return null;
}
@Override
public WebSocketSession getSession()
{
return null;
}
@Override
public String getSubProtocol()
{
return null;
}
@Override
public void incomingError(WebSocketException e)
{
@ -156,11 +139,6 @@ public class LocalWebSocketConnection implements WebSocketConnection, LogicalCon
{
}
@Override
public void ping(ByteBuffer buf) throws IOException
{
}
@Override
public void resume()
{
@ -193,22 +171,4 @@ public class LocalWebSocketConnection implements WebSocketConnection, LogicalCon
{
return String.format("%s[%s]",LocalWebSocketConnection.class.getSimpleName(),id);
}
@Override
public Future<Void> write(byte[] buf, int offset, int len)
{
return null;
}
@Override
public Future<Void> write(ByteBuffer buffer)
{
return null;
}
@Override
public Future<Void> write(String message)
{
return null;
}
}

View File

@ -26,8 +26,8 @@ import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@ -56,19 +56,19 @@ public class LoadTest
@WebSocket
public static class LoadSocket
{
private WebSocketConnection conn;
private Session session;
public static AtomicLong count = new AtomicLong(0);
@OnWebSocketConnect
public void onConnect(WebSocketConnection conn)
public void onConnect(Session session)
{
this.conn = conn;
this.session = session;
}
@OnWebSocketMessage
public void onWebSocketText(String message)
{
conn.write(message);
session.getRemote().sendStringByFuture(message);
long iter = count.incrementAndGet();
if ((iter % 100) == 0)
{

View File

@ -29,7 +29,6 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.server.examples.MyEchoSocket;
import org.junit.After;
import org.junit.Assert;
@ -42,15 +41,14 @@ public class WebSocketOverSSLTest
private int _port;
private QueuedThreadPool _threadPool;
// private WebSocketClientFactory _wsFactory;
private WebSocketConnection _connection;
private Session _session;
@After
public void destroy() throws Exception
{
if (_connection != null)
if (_session != null)
{
_connection.close();
_session.close();
}
// if (_wsFactory != null)
@ -122,7 +120,7 @@ public class WebSocketOverSSLTest
String message = new String(chars);
for (int i = 0; i < count; ++i)
{
_connection.write(message);
_session.getRemote().sendStringByFuture(message);
}
Assert.assertTrue(clientLatch.await(20,TimeUnit.SECONDS));
@ -166,7 +164,7 @@ public class WebSocketOverSSLTest
clientLatch.countDown();
}
});
_connection.write(message);
_session.getRemote().sendStringByFuture(message);
Assert.assertTrue(serverLatch.await(5,TimeUnit.SECONDS));
Assert.assertTrue(clientLatch.await(5,TimeUnit.SECONDS));

View File

@ -26,7 +26,7 @@ import java.util.Locale;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
@ -36,7 +36,7 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
public class BrowserSocket
{
private static final Logger LOG = Log.getLogger(BrowserSocket.class);
private WebSocketConnection connection;
private Session session;
private final String userAgent;
private final String requestedExtensions;
@ -47,15 +47,15 @@ public class BrowserSocket
}
@OnWebSocketConnect
public void onConnect(WebSocketConnection conn)
public void onConnect(Session session)
{
this.connection = conn;
this.session = session;
}
@OnWebSocketClose
public void onDisconnect(int statusCode, String reason)
{
this.connection = null;
this.session = null;
LOG.info("Closed [{}, {}]",statusCode,reason);
}
@ -114,19 +114,19 @@ public class BrowserSocket
private void writeMessage(String message)
{
if (this.connection == null)
if (this.session == null)
{
LOG.debug("Not connected");
return;
}
if (connection.isOpen() == false)
if (session.isOpen() == false)
{
LOG.debug("Not open");
return;
}
connection.write(message);
session.getRemote().sendStringByFuture(message);
}
private void writeMessage(String format, Object... args)

View File

@ -18,7 +18,9 @@
package org.eclipse.jetty.websocket.server.examples.echo;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@ -29,22 +31,22 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
public class BigEchoSocket
{
@OnWebSocketMessage
public void onBinary(WebSocketConnection conn, byte buf[], int offset, int length)
public void onBinary(Session session, byte buf[], int offset, int length)
{
if (conn.isOpen())
if (session.isOpen())
{
return;
}
conn.write(buf,offset,length);
session.getRemote().sendBytesByFuture(ByteBuffer.wrap(buf,offset,length));
}
@OnWebSocketMessage
public void onText(WebSocketConnection conn, String message)
public void onText(Session session, String message)
{
if (conn.isOpen())
if (session.isOpen())
{
return;
}
conn.write(message);
session.getRemote().sendStringByFuture(message);
}
}

View File

@ -22,7 +22,7 @@ import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket
@ -31,11 +31,11 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket
private static class KeepAlive extends Thread
{
private CountDownLatch latch;
private WebSocketConnection conn;
private Session session;
public KeepAlive(WebSocketConnection conn)
public KeepAlive(Session session)
{
this.conn = conn;
this.session = session;
}
@Override
@ -50,7 +50,7 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket
data.put(new byte[]
{ (byte)1, (byte)2, (byte)3 });
data.flip();
conn.ping(data);
session.getRemote().sendPing(data);
}
}
catch (Exception e)
@ -89,13 +89,13 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket
}
@Override
public void onOpen(WebSocketConnection conn)
public void onOpen(Session session)
{
if (keepAlive == null)
{
keepAlive = new KeepAlive(conn);
keepAlive = new KeepAlive(session);
}
keepAlive.start();
super.onOpen(conn);
super.onOpen(session);
}
}

View File

@ -18,9 +18,10 @@
package org.eclipse.jetty.websocket.server.examples.echo;
import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
@ -31,14 +32,15 @@ public class EchoBroadcastSocket
{
private static final ConcurrentLinkedQueue<EchoBroadcastSocket> BROADCAST = new ConcurrentLinkedQueue<EchoBroadcastSocket>();
protected WebSocketConnection conn;
protected Session session;
@OnWebSocketMessage
public void onBinary(byte buf[], int offset, int len)
{
ByteBuffer data = ByteBuffer.wrap(buf,offset,len);
for (EchoBroadcastSocket sock : BROADCAST)
{
sock.conn.write(buf,offset,len);
sock.session.getRemote().sendBytesByFuture(data.slice());
}
}
@ -49,9 +51,9 @@ public class EchoBroadcastSocket
}
@OnWebSocketConnect
public void onOpen(WebSocketConnection conn)
public void onOpen(Session session)
{
this.conn = conn;
this.session = session;
BROADCAST.add(this);
}
@ -60,7 +62,7 @@ public class EchoBroadcastSocket
{
for (EchoBroadcastSocket sock : BROADCAST)
{
sock.conn.write(text);
sock.session.getRemote().sendStringByFuture(text);
}
}
}

View File

@ -22,7 +22,8 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.api.extensions.Frame;
@ -34,7 +35,7 @@ import org.eclipse.jetty.websocket.api.extensions.Frame;
public class EchoFragmentSocket
{
@OnWebSocketFrame
public void onFrame(WebSocketConnection conn, Frame frame)
public void onFrame(Session session, Frame frame)
{
if (frame.getType().isData())
{
@ -51,18 +52,19 @@ public class EchoFragmentSocket
buf1.limit(half);
buf2.position(half);
RemoteEndpoint remote = session.getRemote();
try
{
switch (frame.getType())
{
case BINARY:
conn.write(buf1);
conn.write(buf2);
remote.sendBytesByFuture(buf1);
remote.sendBytesByFuture(buf2);
break;
case TEXT:
// NOTE: This impl is not smart enough to split on a UTF8 boundary
conn.write(BufferUtil.toUTF8String(buf1));
conn.write(BufferUtil.toUTF8String(buf2));
remote.sendStringByFuture(BufferUtil.toUTF8String(buf1));
remote.sendStringByFuture(BufferUtil.toUTF8String(buf2));
break;
default:
throw new IOException("Unexpected frame type: " + frame.getType());

View File

@ -46,6 +46,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
@ -90,11 +91,13 @@ public class WebSocketChatServlet extends WebSocketServlet implements WebSocketC
public class ChatWebSocket
{
volatile Session session;
volatile RemoteEndpoint remote;
@OnWebSocketConnect
public void onOpen(Session sess)
{
session = sess;
this.session = sess;
this.remote = sess.getRemote();
members.add(this);
}
@ -127,7 +130,7 @@ public class WebSocketChatServlet extends WebSocketServlet implements WebSocketC
}
// Async write the message back.
member.session.getRemote().sendStringByFuture(data);
member.remote.sendStringByFuture(data);
}
}