1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package org.jboss.netty.example.http.websocketx.client;
38
39 import org.jboss.netty.bootstrap.ClientBootstrap;
40 import org.jboss.netty.buffer.ChannelBuffers;
41 import org.jboss.netty.channel.Channel;
42 import org.jboss.netty.channel.ChannelFuture;
43 import org.jboss.netty.channel.ChannelPipeline;
44 import org.jboss.netty.channel.ChannelPipelineFactory;
45 import org.jboss.netty.channel.Channels;
46 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
47 import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
48 import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
49 import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
50 import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
51 import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
52 import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
53 import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
54 import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
55 import org.jboss.netty.handler.ssl.SslContext;
56 import org.jboss.netty.handler.ssl.util.SelfSignedCertificate;
57
58 import java.net.InetSocketAddress;
59 import java.net.URI;
60 import java.util.HashMap;
61 import java.util.Map;
62 import java.util.concurrent.Executors;
63
64 public final class WebSocketClient {
65
66 static final String URL = System.getProperty("url", "ws://127.0.0.1:8080/websocket");
67
68 public static void main(String[] args) throws Exception {
69 URI uri = new URI(URL);
70 String scheme = uri.getScheme() == null? "http" : uri.getScheme();
71 final String host = uri.getHost() == null? "127.0.0.1" : uri.getHost();
72 final int port;
73 if (uri.getPort() == -1) {
74 if ("http".equalsIgnoreCase(scheme)) {
75 port = 80;
76 } else if ("https".equalsIgnoreCase(scheme)) {
77 port = 443;
78 } else {
79 port = -1;
80 }
81 } else {
82 port = uri.getPort();
83 }
84
85 if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
86 System.err.println("Only WS(S) is supported.");
87 return;
88 }
89
90 final boolean ssl = "wss".equalsIgnoreCase(scheme);
91 final SslContext sslCtx;
92 if (ssl) {
93 SelfSignedCertificate ssc = new SelfSignedCertificate();
94 sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
95 } else {
96 sslCtx = null;
97 }
98
99 Map<String, String> customHeaders = new HashMap<String, String>();
100 customHeaders.put("MyHeader", "MyValue");
101
102 ClientBootstrap bootstrap =
103 new ClientBootstrap(
104 new NioClientSocketChannelFactory(
105 Executors.newCachedThreadPool(),
106 Executors.newCachedThreadPool()));
107
108 Channel ch = null;
109 try {
110
111
112
113 final WebSocketClientHandshaker handshaker =
114 new WebSocketClientHandshakerFactory().newHandshaker(
115 uri, WebSocketVersion.V13, null, false, customHeaders);
116
117 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
118 public ChannelPipeline getPipeline() {
119 ChannelPipeline pipeline = Channels.pipeline();
120
121 if (sslCtx != null) {
122 pipeline.addLast("ssl", sslCtx.newHandler(host, port));
123 }
124 pipeline.addLast("decoder", new HttpResponseDecoder());
125 pipeline.addLast("encoder", new HttpRequestEncoder());
126 pipeline.addLast("ws-handler", new WebSocketClientHandler(handshaker));
127 return pipeline;
128 }
129 });
130
131
132 ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
133 ch = future.sync().getChannel();
134
135 handshaker.handshake(ch).sync();
136
137
138 for (int i = 0; i < 1000; i++) {
139 ch.write(new TextWebSocketFrame("Message #" + i));
140 }
141
142
143 ch.write(new PingWebSocketFrame(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
144
145
146 ch.write(new CloseWebSocketFrame());
147
148
149
150 ch.getCloseFuture().sync();
151 } finally {
152 if (ch != null) {
153 ch.close();
154 }
155 bootstrap.releaseExternalResources();
156 }
157 }
158 }