add text and binary frame generators
This commit is contained in:
parent
b61a58ad64
commit
8893b9c96a
|
@ -0,0 +1,21 @@
|
|||
package org.eclipse.jetty.websocket.generator;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.io.ByteBufferPool;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.frames.BinaryFrame;
|
||||
|
||||
public class BinaryFrameGenerator extends FrameGenerator<BinaryFrame>
|
||||
{
|
||||
public BinaryFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy policy)
|
||||
{
|
||||
super(bufferPool, policy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer payload(BinaryFrame binary)
|
||||
{
|
||||
return binary.getData();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package org.eclipse.jetty.websocket.generator;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.io.ByteBufferPool;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketException;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.frames.TextFrame;
|
||||
|
||||
public class TextFrameGenerator extends FrameGenerator<TextFrame>
|
||||
{
|
||||
public TextFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy policy)
|
||||
{
|
||||
super(bufferPool, policy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer payload(TextFrame text)
|
||||
{
|
||||
try
|
||||
{
|
||||
String data = text.getData().toString();
|
||||
ByteBuffer payload = ByteBuffer.allocate(data.length());
|
||||
payload.put(data.getBytes("UTF-8"));
|
||||
return payload;
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
// TODO improve ex handling
|
||||
throw new WebSocketException("text frame was not correctly encoded");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue