|
@@ -1,147 +0,0 @@
|
|
|
-/*
|
|
|
- * Copyright 2012 The Netty Project
|
|
|
- *
|
|
|
- * The Netty Project licenses this file to you under the Apache License,
|
|
|
- * version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
- * with the License. You may obtain a copy of the License at:
|
|
|
- *
|
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
- *
|
|
|
- * Unless required by applicable law or agreed to in writing, software
|
|
|
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
- * License for the specific language governing permissions and limitations
|
|
|
- * under the License.
|
|
|
- */
|
|
|
-//The MIT License
|
|
|
-//
|
|
|
-//Copyright (c) 2009 Carl Bystršm
|
|
|
-//
|
|
|
-//Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
-//of this software and associated documentation files (the "Software"), to deal
|
|
|
-//in the Software without restriction, including without limitation the rights
|
|
|
-//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
-//copies of the Software, and to permit persons to whom the Software is
|
|
|
-//furnished to do so, subject to the following conditions:
|
|
|
-//
|
|
|
-//The above copyright notice and this permission notice shall be included in
|
|
|
-//all copies or substantial portions of the Software.
|
|
|
-//
|
|
|
-//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
-//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
-//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
-//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
-//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
-//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
-//THE SOFTWARE.
|
|
|
-
|
|
|
-package com.persagy.demo.netty.websocket.client;
|
|
|
-
|
|
|
-import io.netty.buffer.ByteBuf;
|
|
|
-import io.netty.channel.*;
|
|
|
-import io.netty.handler.codec.http.FullHttpResponse;
|
|
|
-import io.netty.handler.codec.http.websocketx.*;
|
|
|
-import io.netty.util.CharsetUtil;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
|
|
|
-
|
|
|
- private final WebSocketClientHandshaker handshaker;
|
|
|
- private ChannelPromise handshakeFuture;
|
|
|
-
|
|
|
- public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
|
|
|
- this.handshaker = handshaker;
|
|
|
- }
|
|
|
-
|
|
|
- public ChannelFuture handshakeFuture() {
|
|
|
- return handshakeFuture;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void handlerAdded(ChannelHandlerContext ctx) {
|
|
|
- handshakeFuture = ctx.newPromise();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelActive(ChannelHandlerContext ctx) {
|
|
|
- handshaker.handshake(ctx.channel());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelInactive(ChannelHandlerContext ctx) {
|
|
|
- System.out.println("WebSocket Client disconnected!");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
- Channel ch = ctx.channel();
|
|
|
-
|
|
|
- try {
|
|
|
- readMsg(msg, ctx);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void readMsg(Object msg, ChannelHandlerContext ctx) {
|
|
|
- Channel ch = ctx.channel();
|
|
|
-// if ((msg instanceof FullHttpResponse)) {
|
|
|
-// ctx.fireChannelRead(msg);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// FullHttpResponse x = (FullHttpResponse) msg;
|
|
|
-// System.out.println("---x---------:" + x);
|
|
|
-// ByteBuf buf = (ByteBuf) msg;
|
|
|
-// //创建目标大小的数组
|
|
|
-// byte[] barray = new byte[buf.readableBytes()];
|
|
|
-// //把数据从bytebuf转移到byte[]
|
|
|
-// buf.getBytes(0, barray);
|
|
|
-// //将byte[]转成字符串用于打印
|
|
|
-// String str = new String(barray);
|
|
|
-// System.out.println("------------:" + str);
|
|
|
-// if (true) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
- if (!handshaker.isHandshakeComplete()) {
|
|
|
- try {
|
|
|
- handshaker.finishHandshake(ch, (FullHttpResponse) msg);
|
|
|
- System.out.println("WebSocket Client connected!");
|
|
|
- handshakeFuture.setSuccess();
|
|
|
- } catch (WebSocketHandshakeException e) {
|
|
|
- log.error("WebSocket Client failed to connect",e);
|
|
|
- handshakeFuture.setFailure(e);
|
|
|
- }
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (msg instanceof FullHttpResponse) {
|
|
|
- FullHttpResponse response = (FullHttpResponse) msg;
|
|
|
- throw new IllegalStateException(
|
|
|
- "Unexpected FullHttpResponse (getStatus=" + response.status() +
|
|
|
- ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
|
|
|
- }
|
|
|
-
|
|
|
- WebSocketFrame frame = (WebSocketFrame) msg;
|
|
|
-// String clientMsg = "console.readLine!";
|
|
|
-// WebSocketFrame clientFrame = new TextWebSocketFrame(clientMsg);
|
|
|
- if (frame instanceof TextWebSocketFrame) {
|
|
|
- TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
|
|
|
- System.out.println("WebSocket Client received message: " + textFrame.text());
|
|
|
- } else if (frame instanceof PongWebSocketFrame) {
|
|
|
- System.out.println("WebSocket Client received pong");
|
|
|
- } else if (frame instanceof CloseWebSocketFrame) {
|
|
|
- System.out.println("WebSocket Client received closing");
|
|
|
- ch.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
|
|
- cause.printStackTrace();
|
|
|
- if (!handshakeFuture.isDone()) {
|
|
|
- handshakeFuture.setFailure(cause);
|
|
|
- }
|
|
|
- ctx.close();
|
|
|
- }
|
|
|
-}
|