94
94
import org .jboss .netty .handler .codec .http .HttpResponse ;
95
95
import org .jboss .netty .handler .codec .http .HttpResponseDecoder ;
96
96
import org .jboss .netty .handler .codec .http .HttpVersion ;
97
+ import org .jboss .netty .handler .codec .http .websocketx .BinaryWebSocketFrame ;
97
98
import org .jboss .netty .handler .codec .http .websocketx .CloseWebSocketFrame ;
99
+ import org .jboss .netty .handler .codec .http .websocketx .TextWebSocketFrame ;
98
100
import org .jboss .netty .handler .codec .http .websocketx .WebSocket08FrameDecoder ;
99
101
import org .jboss .netty .handler .codec .http .websocketx .WebSocket08FrameEncoder ;
100
102
import org .jboss .netty .handler .codec .http .websocketx .WebSocketFrame ;
116
118
import java .nio .channels .ClosedChannelException ;
117
119
import java .nio .channels .FileChannel ;
118
120
import java .nio .channels .WritableByteChannel ;
121
+ import java .nio .charset .Charset ;
119
122
import java .security .GeneralSecurityException ;
120
123
import java .security .NoSuchAlgorithmException ;
121
124
import java .util .ArrayList ;
@@ -145,6 +148,7 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
145
148
private static final String WEBSOCKET = "ws" ;
146
149
private static final String WEBSOCKET_SSL = "wss" ;
147
150
private final static Logger log = LoggerFactory .getLogger (NettyAsyncHttpProvider .class );
151
+ private final static Charset UTF8 = Charset .forName ("UTF-8" );
148
152
private final ClientBootstrap plainBootstrap ;
149
153
private final ClientBootstrap secureBootstrap ;
150
154
private final ClientBootstrap webSocketBootstrap ;
@@ -2371,7 +2375,15 @@ public void onClose(ChannelHandlerContext ctx, ChannelStateEvent e) {
2371
2375
}
2372
2376
2373
2377
private final class WebSocketProtocol implements Protocol {
2374
-
2378
+ private static final byte OPCODE_CONT = 0x0 ;
2379
+ private static final byte OPCODE_TEXT = 0x1 ;
2380
+ private static final byte OPCODE_BINARY = 0x2 ;
2381
+ private static final byte OPCODE_UNKNOWN = -1 ;
2382
+
2383
+ protected ChannelBuffer byteBuffer = null ;
2384
+ protected StringBuilder textBuffer = null ;
2385
+ protected byte pendingOpcode = OPCODE_UNKNOWN ;
2386
+
2375
2387
// @Override
2376
2388
public void handle (ChannelHandlerContext ctx , MessageEvent e ) throws Exception {
2377
2389
NettyResponseFuture future = NettyResponseFuture .class .cast (ctx .getAttachment ());
@@ -2448,6 +2460,13 @@ public void handle(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
2448
2460
} else if (e .getMessage () instanceof WebSocketFrame ) {
2449
2461
final WebSocketFrame frame = (WebSocketFrame ) e .getMessage ();
2450
2462
2463
+ if (frame instanceof TextWebSocketFrame ) {
2464
+ pendingOpcode = OPCODE_TEXT ;
2465
+ }
2466
+ else if (frame instanceof BinaryWebSocketFrame ) {
2467
+ pendingOpcode = OPCODE_BINARY ;
2468
+ }
2469
+
2451
2470
HttpChunk webSocketChunk = new HttpChunk () {
2452
2471
private ChannelBuffer content ;
2453
2472
@@ -2473,8 +2492,13 @@ public void setContent(ChannelBuffer content) {
2473
2492
h .onBodyPartReceived (rp );
2474
2493
2475
2494
NettyWebSocket webSocket = NettyWebSocket .class .cast (h .onCompleted ());
2476
- webSocket .onMessage (rp .getBodyPartBytes ());
2477
- webSocket .onTextMessage (frame .getBinaryData ().toString ("UTF-8" ));
2495
+
2496
+ if (pendingOpcode == OPCODE_BINARY ) {
2497
+ webSocket .onBinaryFragment (rp .getBodyPartBytes (),frame .isFinalFragment ());
2498
+ }
2499
+ else {
2500
+ webSocket .onTextFragment (frame .getBinaryData ().toString (UTF8 ),frame .isFinalFragment ());
2501
+ }
2478
2502
2479
2503
if (CloseWebSocketFrame .class .isAssignableFrom (frame .getClass ())) {
2480
2504
try {
0 commit comments