ba28679775
Motivation: The examples have not been updated since long time ago, showing various issues fixed in this commit. Modifications: - Overall simplification to reduce LoC - Use system properties to get options instead of parsing args. - Minimize option validation - Just use System.out/err instead of Logger - Do not pass config as parameters - just access it directly - Move the main logic to main(String[]) instead of creating a new instance meaninglessly - Update netty-build-21 to make checkstyle not complain - Remove 'throws Exception' clause if possible - Line wrap at 120 (previously at 80) - Add an option to enable SSL for most examples - Use ChannelFuture.sync() instead of await() - Use System.out for the actual result. Use System.err otherwise. - Delete examples that are not very useful: - websocket/html5 - websocketx/sslserver - localecho/multithreaded - Add run-example.sh which simplifies launching an example from command line Result: Shorter and simpler examples. A user can focus more on what it actually does than miscellaneous stuff. A user can launch an example very easily.
105 lines
4.6 KiB
Java
105 lines
4.6 KiB
Java
/*
|
|
* 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 org.jboss.netty.example.http.websocketx.client;
|
|
|
|
import org.jboss.netty.channel.Channel;
|
|
import org.jboss.netty.channel.ChannelHandlerContext;
|
|
import org.jboss.netty.channel.ChannelStateEvent;
|
|
import org.jboss.netty.channel.ExceptionEvent;
|
|
import org.jboss.netty.channel.MessageEvent;
|
|
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
|
import org.jboss.netty.handler.codec.http.HttpResponse;
|
|
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
|
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
|
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
|
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|
import org.jboss.netty.util.CharsetUtil;
|
|
|
|
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler {
|
|
|
|
private final WebSocketClientHandshaker handshaker;
|
|
|
|
public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
|
|
this.handshaker = handshaker;
|
|
}
|
|
|
|
@Override
|
|
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
|
|
System.err.println("WebSocket Client disconnected!");
|
|
}
|
|
|
|
@Override
|
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
|
|
Channel ch = ctx.getChannel();
|
|
if (!handshaker.isHandshakeComplete()) {
|
|
handshaker.finishHandshake(ch, (HttpResponse) e.getMessage());
|
|
System.err.println("WebSocket Client connected!");
|
|
return;
|
|
}
|
|
|
|
if (e.getMessage() instanceof HttpResponse) {
|
|
HttpResponse response = (HttpResponse) e.getMessage();
|
|
throw new IllegalStateException(
|
|
"unexpected response (status=" + response.getStatus() +
|
|
", content=" + response.getContent().toString(CharsetUtil.UTF_8) + ')');
|
|
}
|
|
|
|
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
|
|
if (frame instanceof TextWebSocketFrame) {
|
|
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
|
|
System.err.println("WebSocket Client received message: " + textFrame.getText());
|
|
} else if (frame instanceof PongWebSocketFrame) {
|
|
System.err.println("WebSocket Client received pong");
|
|
} else if (frame instanceof CloseWebSocketFrame) {
|
|
System.err.println("WebSocket Client received closing");
|
|
ch.close();
|
|
} else if (frame instanceof PingWebSocketFrame) {
|
|
System.err.println("WebSocket Client received ping, response with pong");
|
|
ch.write(new PongWebSocketFrame(frame.getBinaryData()));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
|
|
e.getCause().printStackTrace();
|
|
e.getChannel().close();
|
|
}
|
|
}
|