websocket.server.RandomResponseGenerator.java
在西疇等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網站制作、成都做網站、外貿營銷網站建設 網站設計制作按需定制制作,公司網站建設,企業(yè)網站建設,品牌網站設計,營銷型網站建設,外貿網站制作,西疇網站建設費用合理。
package websocket.server; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import java.util.Random; import org.apache.log4j.Logger; public class RandomResponseGenerator extends Thread{ private ChannelHandlerContext ctx; private Random random = new Random(); private int messageCount = 10; public RandomResponseGenerator(ChannelHandlerContext ctx){ this.ctx=ctx; } private Logger logger = Logger.getLogger(RandomResponseGenerator.class); public void run() { while(messageCount-->0){ ctx.writeAndFlush(new TextWebSocketFrame("[server] the random value is : "+random.nextInt(20))); try { Thread.sleep(1000); } catch (InterruptedException e) { logger.error("encounter an exception",e); } } } }
websocket.server.HttpRequestHandler.java
package websocket.server; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.FullHttpRequest; import org.apache.log4j.Logger; public class HttpRequestHandler extends SimpleChannelInboundHandler{ private final String wsUri; public HttpRequestHandler(String wsUri) { this.wsUri = wsUri; } private Logger logger = Logger.getLogger(HttpRequestHandler.class); @Override protected void channelRead0(final ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { if(wsUri.equalsIgnoreCase(msg.getUri())){ logger.info("a websocket connection established ... "); logger.info("the request uri is : "+msg.getUri()); new RandomResponseGenerator(ctx).start(); ctx.fireChannelRead(msg.retain()); } } }
websocket.server.TextWebSocketFrameHandler.java
package websocket.server; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import org.apache.log4j.Logger; public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler{ private Logger logger = Logger.getLogger(TextWebSocketFrameHandler.class); @Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { String message = msg.content().toString(io.netty.util.CharsetUtil.UTF_8); logger.info("receive below information from client:\n"+message); ctx.writeAndFlush(new TextWebSocketFrame("[server] receive message ["+message+"] successfully")); } }
websocket.server.WebSocketServerInitializer.java
package websocket.server; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.stream.ChunkedWriteHandler; public class WebSocketServerInitializer extends ChannelInitializer{ @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpObjectAggregator(64*1024)); pipeline.addLast(new HttpRequestHandler("/ws")); pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); pipeline.addLast(new TextWebSocketFrameHandler()); } }
pom.xml
log4j log4j 1.2.14 commons-logging commons-logging 1.1.1 io.netty netty-all 4.0.29.Final
使用HTML5作為websocket前端實現(xiàn)
index.html
Insert title here