|
@@ -1,6 +1,5 @@
|
|
|
package cn.com.ty.chat.neety.server;
|
|
|
|
|
|
-import cn.com.ty.chat.neety.server.handler.WebSocketHandler;
|
|
|
import cn.com.ty.chat.utils.RemotingUtil;
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
import io.netty.channel.*;
|
|
@@ -8,16 +7,21 @@ import io.netty.channel.epoll.Epoll;
|
|
|
import io.netty.channel.epoll.EpollEventLoopGroup;
|
|
|
import io.netty.channel.epoll.EpollServerSocketChannel;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
-import io.netty.handler.codec.http.HttpObjectAggregator;
|
|
|
-import io.netty.handler.codec.http.HttpServerCodec;
|
|
|
-import io.netty.handler.stream.ChunkedWriteHandler;
|
|
|
-import io.netty.handler.timeout.IdleStateHandler;
|
|
|
+import io.netty.handler.ssl.SslContext;
|
|
|
+import io.netty.handler.ssl.SslContextBuilder;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import javax.net.ssl.KeyManagerFactory;
|
|
|
+import javax.net.ssl.SSLException;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.KeyStore;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.UnrecoverableKeyException;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
|
|
|
@Slf4j
|
|
|
public class ChatServer {
|
|
@@ -36,7 +40,7 @@ public class ChatServer {
|
|
|
*
|
|
|
* @throws InterruptedException
|
|
|
*/
|
|
|
- public void start(Integer port, Integer readerIdleTime) throws InterruptedException {
|
|
|
+ public void start(Integer port, Integer readerIdleTime, String sslPath, String sslPassword) throws InterruptedException {
|
|
|
try {
|
|
|
if (useEpoll()) {
|
|
|
bossGroup = new EpollEventLoopGroup();
|
|
@@ -45,6 +49,16 @@ public class ChatServer {
|
|
|
bossGroup = new NioEventLoopGroup();
|
|
|
workerGroup = new NioEventLoopGroup();
|
|
|
}
|
|
|
+
|
|
|
+ //构建ssl,适配wss请求
|
|
|
+ KeyStore keyStore = KeyStore.getInstance("JKS");
|
|
|
+ keyStore.load(new FileInputStream(sslPath), sslPassword.toCharArray());
|
|
|
+
|
|
|
+ KeyManagerFactory keyManagerFactory = null;
|
|
|
+ keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
|
|
|
+ keyManagerFactory.init(keyStore, sslPassword.toCharArray());
|
|
|
+
|
|
|
+ SslContext sslContext = SslContextBuilder.forServer(keyManagerFactory).build();
|
|
|
// netty服务,启动引擎
|
|
|
ServerBootstrap b = new ServerBootstrap();
|
|
|
// 主从模式
|
|
@@ -54,32 +68,26 @@ public class ChatServer {
|
|
|
// 配置信息
|
|
|
.option(ChannelOption.SO_BACKLOG, 1024)// 针对主线程配置
|
|
|
// .childOption(ChannelOption.SO_KEEPALIVE,true);//子线程配置
|
|
|
- // 子线程的处理,Handler
|
|
|
- .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel channel) throws InterruptedException {
|
|
|
- ChannelPipeline pipeline = channel.pipeline();
|
|
|
- // 读、取、读取 心跳检测
|
|
|
-// pipeline.addLast("idle", new ImIdleHandler(2000, 2500, 3000));
|
|
|
- pipeline.addLast("ping", new IdleStateHandler(readerIdleTime, 0, 0, TimeUnit.SECONDS));
|
|
|
- // Http消息编码解码
|
|
|
- pipeline.addLast("http-codec", new HttpServerCodec());
|
|
|
- // 参数含义是消息合并的数据大小,如此代表聚合的消息内容长度不超过65536kb。
|
|
|
- pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
|
|
|
- // WebSocket通信支持
|
|
|
- pipeline.addLast("http-chunked", new ChunkedWriteHandler());
|
|
|
- // http请求的业务逻辑处理 WebSocket服务端Handler
|
|
|
- pipeline.addLast("handler", new WebSocketHandler());
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
|
|
|
+ // 子线程的处理,Handler
|
|
|
+ .childHandler(new MyInitChannel(sslContext, readerIdleTime));
|
|
|
// 等待客户端连接
|
|
|
ChannelFuture future = b.bind(port).sync();
|
|
|
- log.info("websoket-server started successful. port:{} ", port);
|
|
|
+ log.info("websocket-server started successful. port:{} ", port);
|
|
|
future.channel().closeFuture().sync();
|
|
|
- } catch (InterruptedException e) {
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (KeyStoreException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (CertificateException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (SSLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnrecoverableKeyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
bossGroup.shutdownGracefully().sync();
|