Example usage for io.netty.handler.codec.http.websocketx TextWebSocketFrame TextWebSocketFrame

List of usage examples for io.netty.handler.codec.http.websocketx TextWebSocketFrame TextWebSocketFrame

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.websocketx TextWebSocketFrame TextWebSocketFrame.

Prototype

public TextWebSocketFrame() 

Source Link

Document

Creates a new empty text frame.

Usage

From source file:com.zhucode.longio.transport.netty.HttpClientHandler.java

License:Open Source License

private ChannelFuture sendForWebSocket(ChannelHandlerContext ctx, byte[] bytes) {
    TextWebSocketFrame frame = new TextWebSocketFrame();
    frame.content().writeBytes(bytes);/*  w w w  .ja  v a 2s  . c o  m*/
    return ctx.channel().writeAndFlush(frame);
}

From source file:lee.study.handle.WebSocketServerHandler.java

License:Apache License

private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
        handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
        return;/*  w  w w . j  av a 2  s .co m*/
    }
    if (frame instanceof PingWebSocketFrame) {
        ctx.write(new PongWebSocketFrame(frame.content().retain()));
        return;
    }
    if (frame instanceof TextWebSocketFrame) {
        byte[] bts = new byte[frame.content().readableBytes()];
        frame.content().readBytes(bts);
        BoomRequest request = JSON.parseObject(new String(bts), BoomRequest.class);
        if (request.getStatus() == 0) {//ID
            BoomResponse response = new BoomResponse(0, Room.room.get(ctx).getId());
            TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame();
            textWebSocketFrame.content().writeBytes(JSON.toJSONString(response).getBytes());
            ctx.writeAndFlush(textWebSocketFrame);
        } else if (request.getStatus() == 1) {//
            List<Hero> otherHeros = new ArrayList<Hero>();
            for (Map.Entry<ChannelHandlerContext, Hero> entry : Room.room.entrySet()) {
                if (entry.getKey() != ctx) {
                    otherHeros.add(entry.getValue());
                }
            }
            BoomResponse response = new BoomResponse(1, otherHeros);
            for (Map.Entry<ChannelHandlerContext, Hero> entry : Room.room.entrySet()) {
                TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame();
                textWebSocketFrame.content().writeBytes(JSON.toJSONString(response).getBytes());
                entry.getKey().writeAndFlush(textWebSocketFrame);
            }
        } else if (request.getStatus() == 2) {//
            BoomResponse response = new BoomResponse(2, Room.room.get(ctx));
            for (Map.Entry<ChannelHandlerContext, Hero> entry : Room.room.entrySet()) {
                TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame();
                textWebSocketFrame.content().writeBytes(JSON.toJSONString(response).getBytes());
                entry.getKey().writeAndFlush(textWebSocketFrame);
            }
        } else {//
            Hero currUser = Room.room.get(ctx);
            currUser.setStatus(request.getBussObj().getStatus());
            BoomResponse response = new BoomResponse(3, currUser);
            //???
            if (request.getBussObj().getStatus() == 0) {
                currUser.setX(request.getBussObj().getX());
                currUser.setY(request.getBussObj().getY());
            }
            for (Map.Entry<ChannelHandlerContext, Hero> entry : Room.room.entrySet()) {
                TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame();
                System.out.println(JSON.toJSONString(response));
                textWebSocketFrame.content().writeBytes(JSON.toJSONString(response).getBytes());
                entry.getKey().writeAndFlush(textWebSocketFrame);
            }
        }
        return;
    }
    if (frame instanceof BinaryWebSocketFrame) {
        // Echo the frame
        ctx.write(frame.retain());
    }
}