netty处理websocket请求demo

爱喝貓的咖啡 发布于 2014/10/28 14:23
阅读 3K+
收藏 2

哪位亲,给看下。
netty v3.9.4
做了个netty接websocket的例子,但是请求类型判断始终是HttpRequet,没有WebSocketFrame请求,开始猜想,websocket要先建立http连接,后来一想websocket貌似不用http。

结果是这样的:
message:class org.jboss.netty.handler.codec.http.DefaultHttpRequest
HttpRequest uri:/websocket
HttpRequest method:GET
message:class org.jboss.netty.handler.codec.http.DefaultHttpRequest
HttpRequest uri:/web4/http11client.jsp?q=a11
HttpRequest method:GET

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
 
public class HttpServerChannelHandler extends SimpleChannelHandler {
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        Channel ch = e.getChannel();
        Object message = e.getMessage();
         
        System.out.println("message:"+message.getClass());        
        if(message instanceof WebSocketFrame){
            if(message instanceof TextWebSocketFrame){
                TextWebSocketFrame req = (TextWebSocketFrame) message;
                System.out.println("wsRequest text:"+req.getText());
            }
        }else if(message instanceof HttpRequest){
            HttpRequest req = (HttpRequest) message;
            System.out.println("HttpRequest uri:"+req.getUri());
            System.out.println("HttpRequest method:"+req.getMethod());
        }
    }
}



import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket00FrameDecoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;

public class HttpServerChannelPipelineFactory  implements ChannelPipelineFactory {

	@Override
	public ChannelPipeline getPipeline() throws Exception {
		ChannelPipeline cp = Channels.pipeline();
		
		cp.addLast("decoder", new HttpRequestDecoder());
//		cp.addLast("decoder", new WebSocket00FrameDecoder());
		cp.addLast("encoder", new HttpResponseEncoder());
//		cp.addLast("downhandler", new HttpServerDownstreamHandler());
//		cp.addLast("uphandler", new HttpServerUpstreamHandler());
		cp.addLast("handler", new HttpServerChannelHandler());
		
		return cp;
	}

}



import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class HttpServer implements Runnable{
	int port = 80;
	
	public HttpServer(int port){
		this.port = port;
	}

	@Override
	public void run() {
		ServerBootstrap b = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));
		b.setPipelineFactory(new HttpServerChannelPipelineFactory());
		b.setOption("child.tcpNoDelay", true);  
		b.setOption("child.keepAlive", true);
		b.bind(new InetSocketAddress(port));
	}
	
	public static void main(String[] args){
		new HttpServer(80).run();
	}
}



<html>
<head>
<script >
function connect() {
	alert('connect');
	var socket;
	if (!window.WebSocket) {
  		window.WebSocket = window.MozWebSocket;
	}

	if (window.WebSocket) {
  		socket = new WebSocket("ws://127.0.0.1/websocket");
  		socket.onmessage = function(event) {
    		var ta = document.getElementById('responseText');
    		ta.value = event.data;
  		};
  
	  	socket.onopen = function(event) {
	    	var ta = document.getElementById('responseText');
	    	ta.value = "Web Socket opened!";
	  	};
	  
	  	socket.onclose = function(event) {
	    	var ta = document.getElementById('responseText');
	    	ta.value = "Web Socket closed"; 
	  	};
 	} else {
 		alert("Your browser does not support Web Socket.");
	}
}
</script>
</head>

<body>
<input type="button" onclick="connect()" value="ws connect"/> <br/><br/>
<input id="responseText" type="text" value="123456"/>
</body>
</html>




加载中
0
雨宣

有点问题

有点不好 是这样的
   
   

 

返回顶部
顶部