<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.cxlgroupId>
<artifactId>cxl-longdada-nettyartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>cxl-longdada-nettyname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
<relativePath />
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>io.nettygroupId>
<artifactId>netty-allartifactId>
<version>4.1.25.Finalversion>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.11version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.4version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-ioartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.41version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>com.github.tobatogroupId>
<artifactId>fastdfs-clientartifactId>
<version>1.26.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
dependency>
<dependency>
<groupId>com.google.zxinggroupId>
<artifactId>javaseartifactId>
<version>3.3.3version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
#fdfs.soTimeout=1501
#fdfs.connectTimeout=601
#fdfs.thumbImage.width=80
#fdfs.thumbImage.height=80
#fdfs.trackerList[0]=192.168.1.70:22122
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/longdada?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=28740000
spring.datasource.hikari.connection-test-query=SELECT 1
mybatis.type-aliases-package=com.cxl.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
mapper.mappers=com.cxl.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
server.port=8080
server.tomcat.uri-encoding=UTF-8
package com.cxl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@ComponentScan(basePackages= {"com.cxl"})
@MapperScan(basePackages= {"com.cxl.mapper"})
public class CxlLongdadaNettyApplication {
public static void main(String[] args) {
SpringApplication.run(CxlLongdadaNettyApplication.class, args);
}
}
package com.cxl;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import com.cxl.netty.WSServer;
@Component
public class NettyBooter implements ApplicationListener<ContextRefreshedEvent>{
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null) {
try {
WSServer.getInstance().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
WSServer.java
package com.cxl.netty;
import org.springframework.stereotype.Component;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
@Component
public class WSServer {
//单例
private static class SingletionWSServer{
static final WSServer INSTANCE = new WSServer();
}
public static WSServer getInstance() {
return SingletionWSServer.INSTANCE;
}
private EventLoopGroup mainGroup;
private EventLoopGroup subGroup;
private ServerBootstrap serverBootstrap;
private ChannelFuture channelFuture;
public WSServer() {
mainGroup = new NioEventLoopGroup();
subGroup = new NioEventLoopGroup();
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(mainGroup,subGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new WSServerInitialzer());
}
//开启服务器
public void start() {
this.channelFuture = serverBootstrap.bind(8077);
System.err.println("netty websocket server run successfully!!!");
}
}
ChatHandler.java
package com.cxl.netty;
import java.time.LocalDateTime;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.local.LocalChannel;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
/**
* 处理消息的handler
* @author mingliang
*TextWebSocketFrame websocket专门处理文本的对象 ,frame是载体
*/
public class ChatHandler extends SimpleChannelInboundHandler<TextWebSocketFrame>{
//管理所有客户端的channel
private static ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
String content = msg.text(); // 获取客户端传过来的消息内容
System.out.println("msg : "+content);
for (Channel channel : clients) {
channel.writeAndFlush(
new TextWebSocketFrame
("[服务器在:]"+LocalDateTime.now()+
",接收到消息为:"+content));
}
//以下写法和for循环一致
// clients.writeAndFlush(new TextWebSocketFrame
// ("[服务器在:]"+LocalDateTime.now()+
// ",接收到消息为:"+content));
}
//用户进入
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
clients.add(ctx.channel());
System.out.println("添加cid" + ctx.channel().id());clients.add(ctx.channel());
}
//用户离开
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
clients.remove(ctx.channel());
System.out.println("移除cid" + ctx.channel().id());
System.out.println("移除长id" + ctx.channel().id().asLongText());
System.out.println("移除短id" + ctx.channel().id().asShortText());
}
}
WSServerInitialzer.java
package com.cxl.netty;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
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 WSServerInitialzer extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
//编解码器
pipeline.addLast(new HttpServerCodec());
//对大数据流处理
pipeline.addLast(new ChunkedWriteHandler());
// 对httpmessage进行聚合,聚合成fullhttprequest或fullhttpresponse
// 几乎在netty中的编程都会使用到此hanler
pipeline.addLast(new HttpObjectAggregator(1024*64));
//===================以上是用于支持http协议===================
//websocket服务器处理的协议,用于指定给客户端连接访问的路由
pipeline.addLast(
new WebSocketServerProtocolHandler("/ws"));
//自定义拦截器
pipeline.addLast(new ChatHandler());
}
}
更多干货,关注微信公众号