boost::filesystem遇到的坑

1. 曾遇到C++14不支持std::filesystem, 尝试在cmake里增加同时对C++17的支持,未成功,最后将std::filesystem改为boost::filesystem即可。

2. 文件夹路径被一个进程正在访问和写入,再起一个进程删除话程序就会闪退,解决办法就是增加try catch机制

if(!boost::filesystem::remove_all(dir))

boost::filesystem的try catch

try
{
    //代码段
}
catch(const boost::filesystem::filesystem_error &e) {
    //代码段
}

您好,以下是Spring Boot集成Netty实现WebSocket消息推送的示例代码: 1. 首先,在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.25.Final</version> </dependency> ``` 2. 创建WebSocket处理器类: ``` @Component @ChannelHandler.Sharable public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class); private WebSocketServerHandshaker handshaker; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("WebSocket连接已建立"); WebSocketManager.add(ctx.channel()); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { logger.info("WebSocket连接已关闭"); WebSocketManager.remove(ctx.channel()); } @Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { logger.info("收到消息:" + msg.text()); // 处理消息 // ... } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.error("WebSocket异常", cause); ctx.close(); } public void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); return; } WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } } private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } } private static String getWebSocketLocation(FullHttpRequest req) { String location = req.headers().get(HttpHeaderNames.HOST) + "/websocket"; return "ws://" + location; } } ``` 3. 创建WebSocket管理器类: ``` @Component public class WebSocketManager { private static final Logger logger = LoggerFactory.getLogger(WebSocketManager.class); private static final List<Channel> channels = new CopyOnWriteArrayList<>(); public static void add(Channel channel) { channels.add(channel); } public static void remove(Channel channel) { channels.remove(channel); } public static void broadcast(String message) { logger.info("广播消息:" + message); TextWebSocketFrame frame = new TextWebSocketFrame(message); for (Channel channel : channels) { if (channel.isActive()) { channel.writeAndFlush(frame); } } } } ``` 4. 创建WebSocket配置类: ``` @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Autowired private WebSocketHandler webSocketHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler, "/websocket").setAllowedOrigins("*"); } } ``` 5. 在需要推送消息的地方调用WebSocketManager的broadcast方法即可: ``` WebSocketManager.broadcast("Hello, WebSocket!"); ``` 希望这个示例能够帮助到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值