本文整理汇总了Java中io.netty.handler.codec.http.websocketx.WebSocketFrame类的典型用法代码示例。如果您正苦于以下问题:Java WebSocketFrame类的具体用法?Java WebSocketFrame怎么用?Java WebSocketFrame使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocketFrame类属于io.netty.handler.codec.http.websocketx包,在下文中一共展示了WebSocketFrame类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format(
"Channel %s received %s", ctx.channel().hashCode(), StringUtil.simpleClassName(frame)));
}
if (frame instanceof CloseWebSocketFrame) {
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
} else if (frame instanceof PingWebSocketFrame) {
ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()), ctx.voidPromise());
} else if (frame instanceof TextWebSocketFrame) {
ctx.write(frame, ctx.voidPromise());
} else if (frame instanceof BinaryWebSocketFrame) {
ctx.write(frame, ctx.voidPromise());
} else if (frame instanceof ContinuationWebSocketFrame) {
ctx.write(frame, ctx.voidPromise());
} else if (frame instanceof PongWebSocketFrame) {
frame.release();
// Ignore
} else {
throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass()
.getName()));
}
}
示例2: decode
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame webSocketFrame, List<Object> list) throws Exception {
ByteBuf byteBuf = webSocketFrame.content();
Proto proto = new Proto();
proto.setPacketLen(byteBuf.readInt());
proto.setHeaderLen(byteBuf.readShort());
proto.setVersion(byteBuf.readShort());
proto.setOperation(byteBuf.readInt());
proto.setSeqId(byteBuf.readInt());
if (proto.getPacketLen() > proto.getHeaderLen()) {
byte[] bytes = new byte[proto.getPacketLen() - proto.getHeaderLen()];
byteBuf.readBytes(bytes);
proto.setBody(bytes);
}
list.add(proto);
logger.debug("decode: {}", proto);
}
示例3: decode
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
ByteBuf buf = frame.content().order(ByteOrder.LITTLE_ENDIAN);
if (buf.capacity() < 1) {
// Discard empty messages
return;
}
buf.resetReaderIndex();
int packetId = buf.readUnsignedByte();
Packet packet = reg.SERVERBOUND.constructPacket(packetId);
if (packet == null) {
throw new UnknownPacketException("Unknown packet ID: " + packetId);
}
Server.log.finest("Received packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") from " + ctx.channel().remoteAddress());
packet.readData(buf);
out.add(packet);
}
示例4: handlerWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private void handlerWebSocketFrame(ChannelHandlerContext ctx,
WebSocketFrame frame) {
// 判断是否关闭链路的指令
if (frame instanceof CloseWebSocketFrame) {
socketServerHandshaker.close(ctx.channel(),
(CloseWebSocketFrame) frame.retain());
}
// 判断是否ping消息
if (frame instanceof PingWebSocketFrame) {
ctx.channel().write(
new PongWebSocketFrame(frame.content().retain()));
return;
}
// 本例程仅支持文本消息,不支持二进制消息
if (!(frame instanceof TextWebSocketFrame)) {
throw new UnsupportedOperationException(String.format(
"%s frame types not supported", frame.getClass().getName()));
}
// 返回应答消息
String request = ((TextWebSocketFrame) frame).text();
System.out.println("服务端收到:" + request);
TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString()
+ ctx.channel().id() + ":" + request);
// 群发
group.writeAndFlush(tws);
}
示例5: decode
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
ByteBuf buf = frame.content().order(ByteOrder.LITTLE_ENDIAN);
if (buf.capacity() < 1) {
// Discard empty messages
return;
}
buf.resetReaderIndex();
int packetId = buf.readUnsignedByte();
Packet packet = PacketRegistry.CLIENT2SERVER.constructPacket(packetId);
if (packet == null) {
_log.info("Unknown packet ID: " + packetId + ", user disconected!");
ctx.disconnect();
return;
}
Log.logDebug("Received packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") from " + ctx.channel().remoteAddress());
packet.readData(buf);
out.add(packet);
}
示例6: decode
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@SuppressWarnings("deprecation")
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
ByteBuf buf = frame.content().order(ByteOrder.BIG_ENDIAN);
if (buf.capacity() < 1) {
// Discard empty messages
return;
}
buf.resetReaderIndex();
int packetId = buf.readUnsignedByte();
Packet packet = PacketRegistry.SERVERBOUND.constructPacket(packetId);
if (packet == null) {
return;
}
ClitherServer.log.finest("Received packet " + " (" + packet.getClass().getSimpleName() + ") from " + ctx.channel().remoteAddress());
packet.readData(buf);
out.add(packet);
}
示例7: handleFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private void handleFrame(Channel channel, WebSocketFrame frame, WebSocketUpgradeHandler handler, NettyWebSocket webSocket) throws Exception {
if (frame instanceof CloseWebSocketFrame) {
Channels.setDiscard(channel);
CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
webSocket.onClose(closeFrame.statusCode(), closeFrame.reasonText());
} else {
ByteBuf buf = frame.content();
if (buf != null && buf.readableBytes() > 0) {
HttpResponseBodyPart part = config.getResponseBodyPartFactory().newResponseBodyPart(buf, frame.isFinalFragment());
handler.onBodyPartReceived(part);
if (frame instanceof BinaryWebSocketFrame) {
webSocket.onBinaryFragment(part);
} else if (frame instanceof TextWebSocketFrame) {
webSocket.onTextFragment(part);
} else if (frame instanceof PingWebSocketFrame) {
webSocket.onPing(part);
} else if (frame instanceof PongWebSocketFrame) {
webSocket.onPong(part);
}
}
}
}
示例8: decodeWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private Message decodeWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
// Check for closing frame
if (frame instanceof CloseWebSocketFrame) {
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
return null;
}
if (frame instanceof PingWebSocketFrame) {
ctx.write(new PongWebSocketFrame(frame.content().retain()));
return null;
}
if (frame instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
return parseMessage(textFrame.content());
}
if (frame instanceof BinaryWebSocketFrame) {
BinaryWebSocketFrame binFrame = (BinaryWebSocketFrame) frame;
return parseMessage(binFrame.content());
}
log.warn("Message format error: " + frame);
return null;
}
示例9: handleWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
// Check for closing frame
if (frame instanceof CloseWebSocketFrame) {
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
return;
}
if (frame instanceof PingWebSocketFrame) {
ctx.write(new PongWebSocketFrame(frame.content().retain()));
return;
}
if (frame instanceof TextWebSocketFrame) {
// Echo the frame
ctx.write(frame.retain());
return;
}
if (frame instanceof BinaryWebSocketFrame) {
// Echo the frame
ctx.write(frame.retain());
return;
}
}
示例10: channelRead0
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("client channelRead0 "+ctx);
Channel ch = ctx.channel();
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
System.out.println("WebSocket Client connected!");
handshakeFuture.setSuccess();
}
if(msg instanceof WebSocketFrame){
WebSocketFrame frame = (WebSocketFrame)msg;
if(frame instanceof BinaryWebSocketFrame){
handleWebSocketFrame(ctx, frame);
}
return;
}
return;
}
示例11: channelRead0
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("client channelRead0 "+ctx);
Channel ch = ctx.channel();
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
System.out.println("WebSocket Client connected!");
handshakeFuture.setSuccess();
}
if(msg instanceof WebSocketFrame){
WebSocketFrame frame = (WebSocketFrame)msg;
if(frame instanceof BinaryWebSocketFrame){
handleWebSocketFrame(ctx, frame);
}
return;
}
sendRealTimeMessageTest(ctx);
return;
}
示例12: handleWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
// Check for closing frame
if (frame instanceof CloseWebSocketFrame) {
addTraceForFrame(frame, "close");
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
return;
}
if (frame instanceof PingWebSocketFrame) {
addTraceForFrame(frame, "ping");
ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
return;
}
if (!(frame instanceof TextWebSocketFrame)) {
throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass()
.getName()));
}
// todo [om] think about BinaryWebsocketFrame
handleTextWebSocketFrameInternal((TextWebSocketFrame) frame, ctx);
}
开发者ID:spring-cloud,项目名称:spring-cloud-stream-app-starters,代码行数:22,代码来源:WebsocketSinkServerHandler.java
示例13: channelRead0
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame)
{
// ping and pong frames already handled
if (frame instanceof TextWebSocketFrame)
{
// Send the uppercase string back.
String request = ((TextWebSocketFrame) frame).text();
handler.onText(new WsChannelImpl(ctx.channel()), request);
}
else
{
String message = "unsupported frame type: " + frame.getClass().getName();
throw new UnsupportedOperationException(message);
}
}
示例14: channelRead0
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext context, Object message) throws Exception {
final Channel channel = context.channel();
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(channel, (FullHttpResponse) message);
channel.pipeline().addBefore(HANDLER_NAME, "websocket-frame-aggregator", new WebSocketFrameAggregator(64 * 1024));
subscriber.onStart();
return;
}
if (message instanceof FullHttpResponse) {
final FullHttpResponse response = (FullHttpResponse) message;
throw new IllegalStateException(
"Unexpected FullHttpResponse (getStatus=" + response.getStatus() +
", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
}
final WebSocketFrame frame = (WebSocketFrame) message;
if (frame instanceof PingWebSocketFrame) {
context.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame)frame).retain().content()));
} else if (frame instanceof BinaryWebSocketFrame) {
final ByteBufInputStream input = new ByteBufInputStream(((BinaryWebSocketFrame)message).content());
final Envelope envelope = Envelope.ADAPTER.decode(input);
subscriber.onNext(envelope);
}
}
示例15: handleWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame; //导入依赖的package包/类
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
// Check for closing frame
if (frame instanceof CloseWebSocketFrame) {
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
return;
}
if (frame instanceof PingWebSocketFrame) {
ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
return;
}
if (!(frame instanceof TextWebSocketFrame)) {
throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass()
.getName()));
}
// Send the uppercase string back.
String request = ((TextWebSocketFrame) frame).text();
System.err.printf("%s received %s%n", ctx.channel(), request);
ctx.channel().write(new TextWebSocketFrame(request.toUpperCase()));
}