Netty 常用的场景:

    1.充当HTTP 服务器,但Netty 并没有遵循servlet 的标准,反而实现了自己的一套标准进行Http 服务;

    2,RPC 远程调用,在分布式系统中常用的框架

    3.Socket 长连接

 

需要了解的名词

 1.NioEventLoopGroup: 对线程的控制,线程组,事件循环组,死循环,需要定义两个线程组,一个只用于接收客户端的请求,一个负责具体的处理

 2.Bootstrap :对服务启动的封装,可以很轻松的启动服务管道,进行服务的启动

 3.Channel:渠道,每一个请求都会形成一个渠道

 4.initChannel:连接一旦被创建,就会执行initChannel方法

 5.ChannelPipline:连接的管道,里面装有很多的ChannelHandler处理器(第6)

 6.ChannelHandler:定义我们自己的处理器,因为流程进行到这里我们的请求已经进来了,需要我们进行自己的逻辑处理与响应,相当于filter 一样; netty也提供了很多的处理器;

 7.ByteBuf:很重要,以后详细讲解

 8.ChannelGroup :用来存放一个一个的Channel 对象的Channel 组 ,可以通过  new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);进行获取

接下来是示例代码:我们目前刚接触,我们先不用管它里面具体怎么实现的,只要先对netty 有一个全局的认识,知道它的执行流程就可以,知道执行流程后再深入学习每一步的作用,这样我们就不会太迷茫

基本上,所有netty 编程中都会遵循以下三点:

  1. 创建线程组,即EventLoopGroup

  2.进行服务启动类,即bootstrap

  3.针对自己的业务,创建自己的Handler

1.我们的启动类

public class WebSocketService {

    public static void main(String[] args) throws Exception {

        //一样,定义线程组
EventLoopGroup boosGroup =new NioEventLoopGroup();
EventLoopGroup workGroup =new NioEventLoopGroup(); //服务启动类
try
{
ServerBootstrap bootstrap=new ServerBootstrap();
bootstrap.group(boosGroup,workGroup)
.channel(NioServerSocketChannel.class)
//添加日志
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new WebSocketInitializer()); //端口绑定
ChannelFuture channelFuture=bootstrap.bind(9077).sync();
channelFuture.channel().closeFuture().sync();
}
finally {
boosGroup.shutdownGracefully();
workGroup.shutdownGracefully(); } }
}

2.初始化器,用来存放Hander 的

/**
* initializer 初始化器
* @author cys
*
*/
public class WebSocketInitializer extends ChannelInitializer<SocketChannel> { @Override
protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pip =ch.pipeline(); //=====websocket 是基于http协议基础之上的,所以要有http的支持=======//
//编解码器
pip.addLast(new HttpServerCodec());
//对大数据的支持
pip.addLast(new ChunkedWriteHandler());
// 对httpMessage进行聚合,聚合成FullHttpRequest或FullHttpResponse
// 几乎在netty中的编程,都会使用到此hanler
pip.addLast(new HttpObjectAggregator(1024*64)); //This handler does all the heavy lifting for you to run a websocket server.
//这个handler 做了所有的繁重的工作,对于你运行的websocket服务器
//处理了frame 的 close ping pang binary text ,运行中并带到下一个关于你的实现的handler
pip.addLast(new WebSocketServerProtocolHandler("/ws"));//===>ws://localhost:8066/ws //自定义自己的处理类,WebSocketServerProtocolHandler 会将数据帧带到你的handler中 pip.addLast(new WebSocketMyHandler()); } }

3.定义自己的Handler ,也就是自己的逻辑处理

/**
* webSocket 自定义handler
* webSocket 对于数据的传输都是基于Frame(帧)的 ,关于frame 一共有五种类型,遵循RFC标准,netty 实现了这五种标准,所有netty 对websocket 有很好的支持
* 1.BinaryWebSocketFrame
* 2.TextWebSocketFrame
* 3.PongWebSocketFrame
* 4.PingWebSocketFrame
* 5.CloseWebSocketFrame
* ping、pong 的操作,对应的是 WebSocket 的两个控制帧 ==心跳
*
* @author cys
*
*/
public class WebSocketMyHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { //当前的channel 组
private static ChannelGroup channelGroup =new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); @Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { Channel ch =ctx.channel();
//msg.text()接收客户端的消息
String text=msg.text();
System.out.println("收到消息"+text);
for(Channel self :channelGroup) { if(ch ==self) {
ch.writeAndFlush(new TextWebSocketFrame("我:===>"+text)); }
else {
self.writeAndFlush(new TextWebSocketFrame(ch.remoteAddress()+"===>"+text));
}
} } /**
* 当有新的Channel连接 时候会触发
*/
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { Channel ch = ctx.channel(); channelGroup.writeAndFlush(new TextWebSocketFrame(ch.remoteAddress()+"---上线"));
System.out.println(ch.remoteAddress()+"已经连接");
channelGroup.add(ctx.channel());
} /**
* 连接断开的时候触发
*/
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { Channel ch = ctx.channel(); System.out.println(new TextWebSocketFrame(ch.remoteAddress()+"已经断开"));
channelGroup.writeAndFlush(ch.remoteAddress()+"---下线");
} /**
* 出现异常时候触发,关闭当前连接
*/ @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.channel().close();
} }

4.因为websocket 是基于HTML5的,所以客户端就是我们支持websocket的浏览器,

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>web socket连接</title>
</head>
<body> <div>发送消息</div>
<input type="text" id="msgContent"/>
<input type="button" value="send" onclick="CHAT.chat()"/>
<div>接收消息</div>
<div id="reciveMsg" style="background-color:pink"></div> <script type="text/javascript"> window.CHAT ={
socket:null,
init: function(){
if(window.WebSocket){
CHAT.socket =new WebSocket("ws://10.9.121.91:9077/ws");
CHAT.socket.onopen = CHAT.onopen,
CHAT.socket.onclose =CHAT.onclose,
CHAT.socket.onerror =CHAT.onerror,
CHAT.socket.onmessage =CHAT.onmessage }
else{
alert("不支持socket");
}
},
chat: function(){
//消息发送
var content =document.getElementById("msgContent"); if (CHAT.socket != null
&& CHAT.socket != undefined
&& CHAT.socket.readyState == WebSocket.OPEN) {
CHAT.socket.send(content.value);
} else {
// 重连websocket
CHAT.init(); } }
,
//客户端与服务器链接触发
onopen :function(){ console.log("链接已经链接"); },
onclose :function(){
console.log("链接已经关闭");
}, onerror:function(){
console.log("出现错误"); },
onmessage:function(e){ console.log("链接消息");
var reciveMsg =document.getElementById("reciveMsg");
var html =reciveMsg.innerHTML;
reciveMsg.innerHTML =html+"<br/>"+e.data; } } CHAT.init(); </script> </body>
</html>

  

 

 

Netty---入门程序,搭建Websocket 服务器的更多相关文章

  1. netty系列之:使用netty搭建websocket服务器

    目录 简介 netty中的websocket websocket的版本 FrameDecoder和FrameEncoder WebSocketServerHandshaker WebSocketFra ...

  2. 【Netty】(7)---搭建websocket服务器

    [Netty](7)---搭建websocket服务器 说明:本篇博客是基于学习某网有关视频教学. 目的:创建一个websocket服务器,获取客户端传来的数据,同时向客户端发送数据 一.服务端 1. ...

  3. 【C#】MVC项目中搭建WebSocket服务器

    前言 因为项目需要,前端页面中需要不断向后台请求获取一个及一个以上的状态值.最初的方案是为每个状态值请求都建立一个定时器循环定时发起Ajax请求,结果显而 易见.在HTTP1.1协议中,同一客户端浏览 ...

  4. Netty权威指南之Netty入门程序

    package com.hjp.netty.netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Chan ...

  5. 在IIS上搭建WebSocket服务器(三)

    编写客户端代码 1.新建一个*.html文件. ws = new WebSocket('ws://192.168.85.128:8086/Handler1.ashx?user=' + $(" ...

  6. Netty(一)——Netty入门程序

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7447618.html 有兴趣的可先了解下:4种I/O的对比与选型 主要内容包括: Netty开发环境的搭建 ...

  7. Netty入门程序(四)

    maven创建project,引入依赖: <dependency> <groupId>io.netty</groupId> <artifactId>ne ...

  8. 在IIS上搭建WebSocket服务器(一)

    一.搭建环境 1.System.Web.WebSockets需搭建在Windows8及Server2012以上系统的上. 2.在Windows8及Server2012以上系统的上安装IIS和WebSo ...

  9. 在IIS上搭建WebSocket服务器(二)

    服务器端代码编写 1.新建一个ASP.net Web MVC5项目 2.新建一个“一般处理程序” 3.Handler1.ashx代码如下: using System; using System.Col ...

随机推荐

  1. 开发webapp手机返回键 退出问题 摘录

    mui进行手机物理键的监听 确保引入mui 调用以下函数 // android 返回按键处理 androidBack(store, data) { try { mui.init({ keyEventB ...

  2. css- 范围选择

    1.子元素范围选择 举例 .iconList_wr li:nth-child(n + 1):nth-child(-n + 4) { margin-right: 0.6rem; } .iconList_ ...

  3. Python中的元类(译)

    add by zhj: 这是大stackoverflow上一位小白提出的问题,好吧,我承认我也是小白,元类这块我也是好多次想搞明白, 但终究因为太难懂而败下阵来.看了这篇文章明白了许多,再加下啄木鸟社 ...

  4. Linux:条件变量

    条件变量:     条件变量本身不是锁!但它也可以造成线程阻塞.通常与互斥锁配合使用.给多线程提供一个会合的场所. 主要应用函数:     pthread_cond_init函数     pthrea ...

  5. How to Pronounce the Letters NG – No Hard G

    How to Pronounce the Letters NG – No Hard G Share Tweet Share Most of the time when you see the lett ...

  6. cv2对图像进行旋转和放缩变换

    旋转: def get_image_rotation(image): #通用写法,即使传入的是三通道图片依然不会出错 height, width = image.shape[:2] center = ...

  7. 函数练习以及 if else 三元运算

  8. java-学习5

    基础运算与自增自减运算 1.基础运算 int 整型 double 浮点 package day06; public class operation { public static void main( ...

  9. linux下WIFI的AP搜索、连接方法

    wpa_supplicant -Dwext -ieth1 -c/etc/wpa_supplicant.conf  &wpa_cli save_configwpa_cli reconfigure ...

  10. CircleImageView of Android

    [CircleImageView of Android] github上有一个开源的圆角图片项目.地址:https://github.com/hdodenhof/CircleImageView 使用分 ...