refactor all
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package com.imyeyu.api.modules.music.runner;
|
||||
|
||||
import com.imyeyu.api.config.CORSConfig;
|
||||
import com.imyeyu.api.modules.common.entity.Setting;
|
||||
import com.imyeyu.api.modules.common.service.SettingService;
|
||||
import com.imyeyu.api.modules.music.handler.ControllerMessageHandler;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
@@ -24,10 +28,6 @@ import io.netty.handler.stream.ChunkedWriteHandler;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.imyeyu.api.config.CORSConfig;
|
||||
import com.imyeyu.api.modules.common.bean.SettingKey;
|
||||
import com.imyeyu.api.modules.common.service.SettingService;
|
||||
import com.imyeyu.api.modules.music.handler.ControllerMessageHandler;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
@@ -35,7 +35,6 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
@@ -46,7 +45,7 @@ import java.net.InetSocketAddress;
|
||||
* @version 2021-11-03 15:29
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
//@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ControllerBootstrapRunner implements ApplicationRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware {
|
||||
|
||||
@@ -63,6 +62,10 @@ public class ControllerBootstrapRunner implements ApplicationRunner, Application
|
||||
}
|
||||
|
||||
public void run(ApplicationArguments args) {
|
||||
if (settingService.getSystem(Setting.Module.Music.ENABLE).isTrue()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final DefaultFullHttpResponse NFP = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
|
||||
final DefaultFullHttpResponse BRP = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
|
||||
|
||||
@@ -72,7 +75,7 @@ public class ControllerBootstrapRunner implements ApplicationRunner, Application
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
bootstrap.group(boss, worker);
|
||||
bootstrap.channel(NioServerSocketChannel.class);
|
||||
bootstrap.localAddress(new InetSocketAddress(settingService.getAsInt(SettingKey.MUSIC_CONTROLLER_PORT)));
|
||||
bootstrap.localAddress(new InetSocketAddress(settingService.getSystem(Setting.Module.Music.CONTROLLER_PORT).asInteger()));
|
||||
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
|
||||
@Override
|
||||
@@ -86,7 +89,7 @@ public class ControllerBootstrapRunner implements ApplicationRunner, Application
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
|
||||
if (obj instanceof FullHttpRequest req) {
|
||||
if (!req.uri().equals(settingService.getAsString(SettingKey.MUSIC_CONTROLLER_URI))) {
|
||||
if (!req.uri().equals(settingService.getSystem(Setting.Module.Music.CONTROLLER_URI).asString())) {
|
||||
// 访问的路径不是 Web Socket 的端点地址,响应 404
|
||||
ctx.channel().writeAndFlush(NFP).addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
@@ -94,8 +97,8 @@ public class ControllerBootstrapRunner implements ApplicationRunner, Application
|
||||
// 域名限制
|
||||
String reqOrigin = req.headers().get(HttpHeaderNames.ORIGIN);
|
||||
String[] allowOrigin = corsConfig.getAllowOrigin();
|
||||
for (int i = 0; i < allowOrigin.length; i++) {
|
||||
if (allowOrigin[i].equals(reqOrigin)) {
|
||||
for (String s : allowOrigin) {
|
||||
if (s.equals(reqOrigin)) {
|
||||
super.channelRead(ctx, obj);
|
||||
return;
|
||||
}
|
||||
@@ -108,10 +111,10 @@ public class ControllerBootstrapRunner implements ApplicationRunner, Application
|
||||
});
|
||||
pipeline.addLast(new WebSocketServerCompressionHandler());
|
||||
pipeline.addLast(new WebSocketServerProtocolHandler(
|
||||
settingService.getAsString(SettingKey.MUSIC_CONTROLLER_URI),
|
||||
settingService.getSystem(Setting.Module.Music.CONTROLLER_URI).asString(),
|
||||
null,
|
||||
true,
|
||||
settingService.getAsInt(SettingKey.MUSIC_MAX_FRAME_LENGTH)
|
||||
settingService.getSystem(Setting.Module.Music.MAX_FRAME_LENGTH).asLong()
|
||||
)
|
||||
);
|
||||
|
||||
@@ -120,7 +123,7 @@ public class ControllerBootstrapRunner implements ApplicationRunner, Application
|
||||
}
|
||||
});
|
||||
channel = bootstrap.bind().sync().channel();
|
||||
log.info("TimiMusicRC controller service startup with " + settingService.getAsString(SettingKey.MUSIC_CONTROLLER_IP));
|
||||
log.info("TimiMusicRC controller service startup with {}", settingService.getSystem(Setting.Module.Music.CONTROLLER_IP).asString());
|
||||
} catch (Exception e) {
|
||||
log.error("TimiMusicRC controller service startup error", e);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.imyeyu.api.modules.music.runner;
|
||||
|
||||
import com.imyeyu.api.modules.common.entity.Setting;
|
||||
import com.imyeyu.api.modules.common.service.SettingService;
|
||||
import com.imyeyu.api.modules.music.handler.PlayerMessageHandler;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
@@ -12,9 +15,6 @@ import io.netty.handler.codec.string.StringDecoder;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.imyeyu.api.modules.common.bean.SettingKey;
|
||||
import com.imyeyu.api.modules.common.service.SettingService;
|
||||
import com.imyeyu.api.modules.music.handler.PlayerMessageHandler;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
@@ -22,7 +22,6 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -34,7 +33,7 @@ import java.nio.charset.StandardCharsets;
|
||||
* @version 2021-11-03 19:18
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
//@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PlayerBootstrapRunner implements ApplicationRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware {
|
||||
|
||||
@@ -50,19 +49,22 @@ public class PlayerBootstrapRunner implements ApplicationRunner, ApplicationList
|
||||
}
|
||||
|
||||
public void run(ApplicationArguments args) {
|
||||
if (settingService.getSystem(Setting.Module.Music.ENABLE).isTrue()) {
|
||||
return;
|
||||
}
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
boss = new NioEventLoopGroup();
|
||||
worker = new NioEventLoopGroup();
|
||||
try {
|
||||
bootstrap.group(boss, worker);
|
||||
bootstrap.channel(NioServerSocketChannel.class);
|
||||
bootstrap.localAddress(new InetSocketAddress(settingService.getAsInt(SettingKey.MUSIC_PLAYER_PORT)));
|
||||
bootstrap.localAddress(new InetSocketAddress(settingService.getSystem(Setting.Module.Music.PLAYER_PORT).asInteger()));
|
||||
bootstrap.childHandler(new ChannelInitializer<>() {
|
||||
|
||||
@Override
|
||||
protected void initChannel(Channel ch) {
|
||||
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(
|
||||
settingService.getAsInt(SettingKey.MUSIC_MAX_FRAME_LENGTH),
|
||||
settingService.getSystem(Setting.Module.Music.MAX_FRAME_LENGTH).asInteger(),
|
||||
0,
|
||||
4,
|
||||
0,
|
||||
@@ -77,7 +79,7 @@ public class PlayerBootstrapRunner implements ApplicationRunner, ApplicationList
|
||||
|
||||
channel = bootstrap.bind().sync().channel();
|
||||
|
||||
log.info("TimiMusicRC player service startup with " + settingService.getAsString(SettingKey.MUSIC_PLAYER_IP));
|
||||
log.info("TimiMusicRC player service startup with {}", settingService.getSystem(Setting.Module.Music.PLAYER_IP).asString());
|
||||
} catch (Exception e) {
|
||||
log.error("TimiMusicRC player service startup error", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user