中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站反鏈騰訊廣告

網(wǎng)站反鏈,騰訊廣告,經(jīng)典頁游排行榜前十名,可以怎么找回密碼1、關(guān)于Netty Netty 是一個利用 Java 的高級網(wǎng)絡(luò)的能力&#xff0c;隱藏其背后的復(fù)雜性而提供一個易于使用的 API 的客戶端/服務(wù)器框架。 2、Maven依賴 <dependencies><!-- https://mvnrepository.com/artifact/io.netty/netty-all --><dependency><gr…

1、關(guān)于Netty

Netty 是一個利用 Java 的高級網(wǎng)絡(luò)的能力,隱藏其背后的復(fù)雜性而提供一個易于使用的 API 的客戶端/服務(wù)器框架。

2、Maven依賴

<dependencies><!-- https://mvnrepository.com/artifact/io.netty/netty-all --><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.36.Final</version></dependency>
</dependencies>

3、SpringBootApplication

啟動器中需要new一個NettyServer,并顯式調(diào)用啟動netty。

@SpringBootApplication
public class SpringCloudStudyDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudStudyDemoApplication.class,args);try {new NettyServer(12345).start();System.out.println("https://blog.csdn.net/moshowgame");System.out.println("http://127.0.0.1:6688/netty-websocket/index");}catch(Exception e) {System.out.println("NettyServerError:"+e.getMessage());}}
}

4、NettyServer

啟動的NettyServer,這里進行配置

/*** NettyServer Netty服務(wù)器配置*/
public class NettyServer {private final int port;public NettyServer(int port) {this.port = port;}public void start() throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup group = new NioEventLoopGroup();try {ServerBootstrap sb = new ServerBootstrap();sb.option(ChannelOption.SO_BACKLOG, 1024);sb.group(group, bossGroup) // 綁定線程池.channel(NioServerSocketChannel.class) // 指定使用的channel.localAddress(this.port)// 綁定監(jiān)聽端口.childHandler(new ChannelInitializer<SocketChannel>() { // 綁定客戶端連接時候觸發(fā)操作@Overrideprotected void initChannel(SocketChannel ch) throws Exception {System.out.println("收到新連接");//websocket協(xié)議本身是基于http協(xié)議的,所以這邊也要使用http解編碼器ch.pipeline().addLast(new HttpServerCodec());//以塊的方式來寫的處理器ch.pipeline().addLast(new ChunkedWriteHandler());ch.pipeline().addLast(new HttpObjectAggregator(8192));ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws", null, true, 65536 * 10));ch.pipeline().addLast(new MyWebSocketHandler());}});ChannelFuture cf = sb.bind().sync(); // 服務(wù)器異步創(chuàng)建綁定System.out.println(NettyServer.class + " 啟動正在監(jiān)聽:" + cf.channel().localAddress());cf.channel().closeFuture().sync(); // 關(guān)閉服務(wù)器通道} finally {group.shutdownGracefully().sync(); // 釋放線程池資源bossGroup.shutdownGracefully().sync();}}
}

5、MyChannelHandlerPool

通道組池,管理所有websocket連接

/*** MyChannelHandlerPool* 通道組池,管理所有websocket連接*/
public class MyChannelHandlerPool {public MyChannelHandlerPool(){}public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
}

6、MyWebSocketHandler

處理ws一下幾種情況:

channelActive與客戶端建立連接
channelInactive與客戶端斷開連接
channelRead0客戶端發(fā)送消息處理

/*** NettyServer Netty服務(wù)器配置*/
public class NettyServer {private final int port;public NettyServer(int port) {this.port = port;}public void start() throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup group = new NioEventLoopGroup();try {ServerBootstrap sb = new ServerBootstrap();sb.option(ChannelOption.SO_BACKLOG, 1024);sb.group(group, bossGroup) // 綁定線程池.channel(NioServerSocketChannel.class) // 指定使用的channel.localAddress(this.port)// 綁定監(jiān)聽端口.childHandler(new ChannelInitializer<SocketChannel>() { // 綁定客戶端連接時候觸發(fā)操作@Overrideprotected void initChannel(SocketChannel ch) throws Exception {System.out.println("收到新連接");//websocket協(xié)議本身是基于http協(xié)議的,所以這邊也要使用http解編碼器ch.pipeline().addLast(new HttpServerCodec());//以塊的方式來寫的處理器ch.pipeline().addLast(new ChunkedWriteHandler());ch.pipeline().addLast(new HttpObjectAggregator(8192));ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws", "WebSocket", true, 65536 * 10));ch.pipeline().addLast(new MyWebSocketHandler());}});ChannelFuture cf = sb.bind().sync(); // 服務(wù)器異步創(chuàng)建綁定System.out.println(NettyServer.class + " 啟動正在監(jiān)聽:" + cf.channel().localAddress());cf.channel().closeFuture().sync(); // 關(guān)閉服務(wù)器通道} finally {group.shutdownGracefully().sync(); // 釋放線程池資源bossGroup.shutdownGracefully().sync();}}
}

7、socket.html

主要是連接ws,發(fā)送消息,以及消息反饋

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Netty-Websocket</title><script type="text/javascript">// by zhengkai.blog.csdn.netvar socket;if(!window.WebSocket){window.WebSocket = window.MozWebSocket;}if(window.WebSocket){socket = new WebSocket("ws://127.0.0.1:12345/ws");socket.onmessage = function(event){var ta = document.getElementById('responseText');ta.value += event.data+"\r\n";};socket.onopen = function(event){var ta = document.getElementById('responseText');ta.value = "Netty-WebSocket服務(wù)器。。。。。。連接 \r\n";};socket.onclose = function(event){var ta = document.getElementById('responseText');ta.value = "Netty-WebSocket服務(wù)器。。。。。。關(guān)閉 \r\n";};}else{alert("您的瀏覽器不支持WebSocket協(xié)議!");}function send(message){if(!window.WebSocket){return;}if(socket.readyState == WebSocket.OPEN){socket.send(message);}else{alert("WebSocket 連接沒有建立成功!");}}</script>
</head>
<body>
<form onSubmit="return false;"><label>ID</label><input type="text" name="uid" value="${uid!!}" /> <br /><label>TEXT</label><input type="text" name="message" value="這里輸入消息" /> <br /><br /> <input type="button" value="發(fā)送ws消息"onClick="send(this.form.uid.value+':'+this.form.message.value)" /><hr color="black" /><h3>服務(wù)端返回的應(yīng)答消息</h3><textarea id="responseText" style="width: 1024px;height: 300px;"></textarea>
</form>
</body>
</html>

8、Controller

寫好了html當(dāng)然還需要一個controller來引導(dǎo)頁面。

@RestController
public class IndexController {@GetMapping("/index")public ModelAndView index(){ModelAndView mav=new ModelAndView("socket");mav.addObject("uid", RandomUtil.randomNumbers(6));return mav;}
}

9、改造netty支持url參數(shù)

1.首先,調(diào)整一下加載handler的順序,優(yōu)先MyWebSocketHandler在WebSocketServerProtocolHandler之上。

ch.pipeline().addLast(new MyWebSocketHandler());
ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws", null, true, 65536 * 10));

2.其次,改造MyWebSocketHandler 的channelRead方法,首次連接會是一個FullHttpRequest類型,可以通過FullHttpRequest.uri()獲取完整ws的URL地址,之后接受信息的話,會是一個TextWebSocketFrame類型。

public class MyWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println("與客戶端建立連接,通道開啟!");//添加到channelGroup通道組MyChannelHandlerPool.channelGroup.add(ctx.channel());}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {System.out.println("與客戶端斷開連接,通道關(guān)閉!");//添加到channelGroup 通道組MyChannelHandlerPool.channelGroup.remove(ctx.channel());}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {//首次連接是FullHttpRequest,處理參數(shù) by zhengkai.blog.csdn.netif (null != msg && msg instanceof FullHttpRequest) {FullHttpRequest request = (FullHttpRequest) msg;String uri = request.uri();Map paramMap=getUrlParams(uri);System.out.println("接收到的參數(shù)是:"+JSON.toJSONString(paramMap));//如果url包含參數(shù),需要處理if(uri.contains("?")){String newUri=uri.substring(0,uri.indexOf("?"));System.out.println(newUri);request.setUri(newUri);}}else if(msg instanceof TextWebSocketFrame){//正常的TEXT消息類型TextWebSocketFrame frame=(TextWebSocketFrame)msg;System.out.println("客戶端收到服務(wù)器數(shù)據(jù):" +frame.text());sendAllMessage(frame.text());}super.channelRead(ctx, msg);}@Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {}private void sendAllMessage(String message){//收到信息后,群發(fā)給所有channelMyChannelHandlerPool.channelGroup.writeAndFlush( new TextWebSocketFrame(message));}private static Map getUrlParams(String url){Map<String,String> map = new HashMap<>();url = url.replace("?",";");if (!url.contains(";")){return map;}if (url.split(";").length > 0){String[] arr = url.split(";")[1].split("&");for (String s : arr){String key = s.split("=")[0];String value = s.split("=")[1];map.put(key,value);}return  map;}else{return map;}}
}

3.html中的ws地址也進行改造

socket = new WebSocket("ws://127.0.0.1:12345/ws?uid=666&gid=777");

4.改造后控制臺輸出情況

收到新連接
與客戶端建立連接,通道開啟!
接收到的參數(shù)是:{"uid":"666","gid":"777"}
/ws
客戶端收到服務(wù)器數(shù)據(jù):142531:這里輸入消息
客戶端收到服務(wù)器數(shù)據(jù):142531:這里輸入消息
客戶端收到服務(wù)器數(shù)據(jù):142531:這里輸入消息
failed: WebSocket opening handshake timed out
http://www.risenshineclean.com/news/22119.html

相關(guān)文章:

  • 網(wǎng)絡(luò)app制作網(wǎng)站有哪些內(nèi)容app開發(fā)流程
  • 做女朋友的網(wǎng)站qq群引流推廣軟件
  • wordpress 一站多主題國內(nèi)優(yōu)秀網(wǎng)頁設(shè)計賞析
  • 與做機器人有關(guān)的網(wǎng)站軟件開發(fā)需要學(xué)什么
  • iis 網(wǎng)站目錄權(quán)限網(wǎng)站制作維護
  • 惠州seo推廣外包北京百度關(guān)鍵詞優(yōu)化
  • icp備案網(wǎng)站更名整站優(yōu)化多少錢
  • 做家具的企業(yè)網(wǎng)站最吸引人的營銷廣告詞
  • 免費游戲網(wǎng)頁入口西安網(wǎng)站seo外包
  • 深圳雙語網(wǎng)站制作網(wǎng)站的seo是什么意思
  • 怎么做裝修網(wǎng)站seo優(yōu)化招商
  • 家裝建材網(wǎng)購平臺推薦seo外鏈平臺
  • 做網(wǎng)站廠家廣告策劃公司
  • 做網(wǎng)站客戶沒有付定金深圳網(wǎng)站優(yōu)化推廣
  • 前端開發(fā)培訓(xùn)機構(gòu)成都西安官網(wǎng)seo
  • 畢業(yè)設(shè)計做系統(tǒng)跟做網(wǎng)站哪個容易打廣告去哪個平臺免費
  • 香港做批發(fā)的網(wǎng)站有哪些手續(xù)合肥做網(wǎng)站哪家好
  • 音樂播放網(wǎng)站開發(fā)pc端設(shè)計師網(wǎng)站
  • 論壇門戶網(wǎng)站建設(shè)seo文章外包
  • 新網(wǎng)站快速提高排名cdq百度指數(shù)
  • 今天長沙做營銷推廣seo
  • app商城網(wǎng)站開發(fā)百度sem推廣具體做什么
  • 網(wǎng)站沒備案怎么做加速谷歌推廣外貿(mào)建站
  • 西安手機網(wǎng)站建設(shè)動力無限福州百度推廣優(yōu)化排名
  • 淄博網(wǎng)站建設(shè)服務(wù)商發(fā)帖子最好的幾個網(wǎng)站
  • 好文案網(wǎng)站seo范疇有哪些
  • 秦皇島網(wǎng)站設(shè)計seo引擎優(yōu)化怎么做
  • 重慶觀音橋旅游攻略寧波seo的公司聯(lián)系方式
  • 怎么優(yōu)化網(wǎng)站每日新聞
  • 網(wǎng)站開發(fā) 需求清單英文谷歌優(yōu)化