|
| 1 | +package org.feuyeux.websocket; |
| 2 | + |
| 3 | +import static org.feuyeux.websocket.config.EchoConfig.*; |
| 4 | + |
| 5 | +import io.netty.bootstrap.Bootstrap; |
| 6 | +import io.netty.buffer.ByteBuf; |
| 7 | +import io.netty.channel.Channel; |
| 8 | +import io.netty.channel.EventLoopGroup; |
| 9 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 10 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 11 | +import io.netty.handler.codec.http.DefaultHttpHeaders; |
| 12 | +import io.netty.handler.codec.http.websocketx.*; |
| 13 | +import java.net.URI; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | +import org.feuyeux.websocket.client.WebSocketClientHandler; |
| 16 | +import org.feuyeux.websocket.client.WebSocketClientInitializer; |
| 17 | +import org.feuyeux.websocket.codec.EchoRequestCodec; |
| 18 | +import org.feuyeux.websocket.info.EchoRequest; |
| 19 | + |
| 20 | +/** |
| 21 | + * Echo Client <a |
| 22 | + * href="https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java">Sample</a> |
| 23 | + */ |
| 24 | +@Slf4j |
| 25 | +public class HelloClient { |
| 26 | + |
| 27 | + public Channel open(EventLoopGroup group) { |
| 28 | + String URL; |
| 29 | + if (SSL) { |
| 30 | + URL = String.format("wss://%s:9899%s", host, WEBSOCKET_PATH); |
| 31 | + } else { |
| 32 | + URL = String.format("ws://%s:9898%s", host, WEBSOCKET_PATH); |
| 33 | + } |
| 34 | + URI uri = URI.create(URL); |
| 35 | + // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. |
| 36 | + // If you change it to V00, ping is not supported and remember to change |
| 37 | + // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. |
| 38 | + WebSocketClientHandshaker handShaker = |
| 39 | + WebSocketClientHandshakerFactory.newHandshaker( |
| 40 | + uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()); |
| 41 | + final WebSocketClientHandler clientHandler = new WebSocketClientHandler(handShaker); |
| 42 | + final WebSocketClientInitializer clientInitializer = |
| 43 | + new WebSocketClientInitializer(clientHandler); |
| 44 | + |
| 45 | + Bootstrap b = new Bootstrap(); |
| 46 | + b.group(group).channel(NioSocketChannel.class).handler(clientInitializer); |
| 47 | + try { |
| 48 | + log.info("Connecting to server({})", URL); |
| 49 | + Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel(); |
| 50 | + clientHandler.handshakeFuture().sync(); |
| 51 | + return ch; |
| 52 | + } catch (Exception e) { |
| 53 | + log.error("", e); |
| 54 | + } |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + public void close(Channel ch) throws InterruptedException { |
| 59 | + log.info("Closing the connection"); |
| 60 | + ch.writeAndFlush(new CloseWebSocketFrame()); |
| 61 | + ch.closeFuture().sync(); |
| 62 | + } |
| 63 | + |
| 64 | + public void send(Channel ch) { |
| 65 | + log.info("Sending messages"); |
| 66 | + sendBinary(ch, new EchoRequest(81, "わかった")); |
| 67 | + sendBinary(ch, new EchoRequest(82, "알았어")); |
| 68 | + sendBinary(ch, new EchoRequest(44, "Got it")); |
| 69 | + sendBinary(ch, new EchoRequest(33, "Je l'ai")); |
| 70 | + sendBinary(ch, new EchoRequest(7, "Понял")); |
| 71 | + sendBinary(ch, new EchoRequest(30, "Το έπιασα")); |
| 72 | + sendText(ch, "ru:Большое спасибо"); |
| 73 | + sendText(ch, "fr:Merci beaucoup"); |
| 74 | + sendText(ch, "es:Muchas Gracias"); |
| 75 | + sendText(ch, "ar:" + "شكرا جزيلا"); |
| 76 | + sendText(ch, "he:" + "תודה רבה"); |
| 77 | + log.info("Complete to send"); |
| 78 | + } |
| 79 | + |
| 80 | + public void sendText(Channel ch, final String text) { |
| 81 | + TextWebSocketFrame textFrame = new TextWebSocketFrame(text); |
| 82 | + ch.writeAndFlush(textFrame); |
| 83 | + } |
| 84 | + |
| 85 | + public void sendBinary(Channel ch, final EchoRequest echoRequest) { |
| 86 | + ByteBuf byteBuf = EchoRequestCodec.encode(echoRequest); |
| 87 | + BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame(byteBuf); |
| 88 | + ch.writeAndFlush(binaryFrame); |
| 89 | + } |
| 90 | + |
| 91 | + public static void main(String[] args) throws Exception { |
| 92 | + EventLoopGroup group = new NioEventLoopGroup(); |
| 93 | + final HelloClient client = new HelloClient(); |
| 94 | + Channel ch = client.open(group); |
| 95 | + if (ch != null) { |
| 96 | + client.send(ch); |
| 97 | + client.close(ch); |
| 98 | + } |
| 99 | + group.shutdownGracefully(); |
| 100 | + } |
| 101 | +} |
0 commit comments