Updating WebSocket JettyListenerEventDriver for new Listeners
+ Basing implementation on instances of WebSocketConnectionListener + Adding support for: * WebSocketListener * WebSocketPartialListener * WebSocketPingPongListener * WebSocketFrameListener
This commit is contained in:
parent
8e7f05190d
commit
77e42fb05a
|
@ -25,12 +25,19 @@ import java.nio.ByteBuffer;
|
|||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketConnectionListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketFrameListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPartialListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPingPongListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
||||
import org.eclipse.jetty.websocket.api.extensions.Frame.Type;
|
||||
import org.eclipse.jetty.websocket.common.CloseInfo;
|
||||
import org.eclipse.jetty.websocket.common.frames.ReadOnlyDelegatedFrame;
|
||||
import org.eclipse.jetty.websocket.common.message.SimpleBinaryMessage;
|
||||
import org.eclipse.jetty.websocket.common.message.SimpleTextMessage;
|
||||
import org.eclipse.jetty.websocket.common.util.Utf8PartialBuilder;
|
||||
|
||||
/**
|
||||
* Handler for {@link WebSocketListener} based User WebSocket implementations.
|
||||
|
@ -38,10 +45,11 @@ import org.eclipse.jetty.websocket.common.message.SimpleTextMessage;
|
|||
public class JettyListenerEventDriver extends AbstractEventDriver
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(JettyListenerEventDriver.class);
|
||||
private final WebSocketListener listener;
|
||||
private final WebSocketConnectionListener listener;
|
||||
private Utf8PartialBuilder utf8Partial;
|
||||
private boolean hasCloseBeenCalled = false;
|
||||
|
||||
public JettyListenerEventDriver(WebSocketPolicy policy, WebSocketListener listener)
|
||||
public JettyListenerEventDriver(WebSocketPolicy policy, WebSocketConnectionListener listener)
|
||||
{
|
||||
super(policy,listener);
|
||||
this.listener = listener;
|
||||
|
@ -50,18 +58,29 @@ public class JettyListenerEventDriver extends AbstractEventDriver
|
|||
@Override
|
||||
public void onBinaryFrame(ByteBuffer buffer, boolean fin) throws IOException
|
||||
{
|
||||
if (activeMessage == null)
|
||||
if (listener instanceof WebSocketListener)
|
||||
{
|
||||
activeMessage = new SimpleBinaryMessage(this);
|
||||
if (activeMessage == null)
|
||||
{
|
||||
activeMessage = new SimpleBinaryMessage(this);
|
||||
}
|
||||
|
||||
appendMessage(buffer,fin);
|
||||
}
|
||||
|
||||
appendMessage(buffer,fin);
|
||||
if (listener instanceof WebSocketPartialListener)
|
||||
{
|
||||
((WebSocketPartialListener)listener).onWebSocketPartialBinary(buffer.slice().asReadOnlyBuffer(),fin);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBinaryMessage(byte[] data)
|
||||
{
|
||||
listener.onWebSocketBinary(data,0,data.length);
|
||||
if (listener instanceof WebSocketListener)
|
||||
{
|
||||
((WebSocketListener)listener).onWebSocketBinary(data,0,data.length);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -96,7 +115,22 @@ public class JettyListenerEventDriver extends AbstractEventDriver
|
|||
@Override
|
||||
public void onFrame(Frame frame)
|
||||
{
|
||||
/* ignore, not supported by WebSocketListener */
|
||||
if (listener instanceof WebSocketFrameListener)
|
||||
{
|
||||
((WebSocketFrameListener)listener).onWebSocketFrame(new ReadOnlyDelegatedFrame(frame));
|
||||
}
|
||||
|
||||
if (listener instanceof WebSocketPingPongListener)
|
||||
{
|
||||
if (frame.getType() == Type.PING)
|
||||
{
|
||||
((WebSocketPingPongListener)listener).onWebSocketPing(frame.getPayload().asReadOnlyBuffer());
|
||||
}
|
||||
else if (frame.getType() == Type.PONG)
|
||||
{
|
||||
((WebSocketPingPongListener)listener).onWebSocketPong(frame.getPayload().asReadOnlyBuffer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -114,18 +148,46 @@ public class JettyListenerEventDriver extends AbstractEventDriver
|
|||
@Override
|
||||
public void onTextFrame(ByteBuffer buffer, boolean fin) throws IOException
|
||||
{
|
||||
if (activeMessage == null)
|
||||
if (listener instanceof WebSocketListener)
|
||||
{
|
||||
activeMessage = new SimpleTextMessage(this);
|
||||
if (activeMessage == null)
|
||||
{
|
||||
activeMessage = new SimpleTextMessage(this);
|
||||
}
|
||||
|
||||
appendMessage(buffer,fin);
|
||||
}
|
||||
|
||||
appendMessage(buffer,fin);
|
||||
if (listener instanceof WebSocketPartialListener)
|
||||
{
|
||||
if (utf8Partial == null)
|
||||
{
|
||||
utf8Partial = new Utf8PartialBuilder();
|
||||
}
|
||||
|
||||
String partial = utf8Partial.toPartialString(buffer);
|
||||
|
||||
((WebSocketPartialListener)listener).onWebSocketPartialText(partial,fin);
|
||||
|
||||
if (fin)
|
||||
{
|
||||
partial = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whole Message event.
|
||||
*
|
||||
* @param message the whole message
|
||||
*/
|
||||
@Override
|
||||
public void onTextMessage(String message)
|
||||
{
|
||||
listener.onWebSocketText(message);
|
||||
if (listener instanceof WebSocketListener)
|
||||
{
|
||||
((WebSocketListener)listener).onWebSocketText(message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
package org.eclipse.jetty.websocket.common.events;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.WebSocketConnectionListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketListener;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
|
||||
|
@ -26,7 +27,7 @@ public class JettyListenerImpl implements EventDriverImpl
|
|||
@Override
|
||||
public EventDriver create(Object websocket, WebSocketPolicy policy)
|
||||
{
|
||||
WebSocketListener listener = (WebSocketListener)websocket;
|
||||
WebSocketConnectionListener listener = (WebSocketConnectionListener)websocket;
|
||||
return new JettyListenerEventDriver(policy,listener);
|
||||
}
|
||||
|
||||
|
@ -39,6 +40,6 @@ public class JettyListenerImpl implements EventDriverImpl
|
|||
@Override
|
||||
public boolean supports(Object websocket)
|
||||
{
|
||||
return (websocket instanceof WebSocketListener);
|
||||
return (websocket instanceof WebSocketConnectionListener);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,7 +165,7 @@ public class EventDriverTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testListener_Text() throws Exception
|
||||
public void testListenerBasic_Text() throws Exception
|
||||
{
|
||||
ListenerBasicSocket socket = new ListenerBasicSocket();
|
||||
EventDriver driver = wrap(socket);
|
||||
|
|
Loading…
Reference in New Issue