netty处理客户端传过来的get、post、websocket数据例子
jopen
11年前
用neety作为http服务器,在服务器端读取客户端传过来的get和post参数和websocket数据例子,netty版本3.6.6。
package com.penngo.http; 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 { private final int port; public HttpServer(int port) { this.port = port; } public void run() { ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new HttpServerPipelineFactory()); bootstrap.bind(new InetSocketAddress(port)); System.out.println("Web socket server started at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); } public static void main(String[] args) { int port; if (args.length > 0) { port = Integer.parseInt(args[0]); } else { port = 8080; } new HttpServer(port).run(); } }
测试代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web Socket Example</title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js" type="text/javascript"></script> <style> textarea { width: 500px; height: 100px; } </style> <script> $(function(){ var Sock = function() { var socket; if (!window.WebSocket) { window.WebSocket = window.MozWebSocket; } if (window.WebSocket) { socket = new WebSocket("ws://localhost:8080/websocket"); socket.onopen = onopen; socket.onmessage = onmessage; socket.onclose = onclose; } else { alert("Your browser does not support Web Socket."); } function onopen(event) { getTextAreaElement().value = "Web Socket opened!"; } function onmessage(event) { appendTextArea(event.data); } function onclose(event) { appendTextArea("Web Socket closed"); } function appendTextArea(newData) { var el = getTextAreaElement(); el.value = el.value + '\n' + newData; } function getTextAreaElement() { return document.getElementById('responseText'); } function send(event) { event.preventDefault(); if (window.WebSocket) { if (socket.readyState == WebSocket.OPEN) { socket.send(event.target.message.value); } else { alert("The socket is not open."); } } } document.forms.inputform.addEventListener('submit', send, false); } window.addEventListener('load', function() { new Sock(); }, false); $("#btnGet").click( function () { $.get("http://localhost:8080/", { q: "John"}, function(data){ $("#responseTextGet").val($("#responseTextGet").val() + data) }); }); $("#btnPost").click( function () { $.post("http://localhost:8080/", { q: "John"}, function(data){ $("#responseTextPost").val($("#responseTextGet").val() + data) }); }); }); </script> </head> <body> <h3>输入内容</h3> <form name="inputform"> <input type="text" name="message" id="message" placeholder="Enter text to be sent" autofocus> <input type="submit" value="发送WebSocket数据"> </form> <h3>服务端返回</h3> <textarea id="responseText"></textarea> <h3>输入内容</h3> <form name="inputform"> <input type="text" name="message" id="messageGet" placeholder="Enter text to be sent" autofocus> <input type="button" value="Get数据" id="btnGet"> </form> <h3>服务端返回</h3> <textarea id="responseTextGet"></textarea> <h3>输入内容</h3> <form name="inputform"> <input type="text" name="message" id="messagePost" placeholder="Enter text to be sent" autofocus> <input type="button" value="Post数据" id="btnPost"> </form> <h3>服务端返回</h3> <textarea id="responseTextPost"></textarea> </body> </html>