Spring-boot集成Netty做websocket服务端
2017-06-21 14:04
351 查看
spring-boot-websocket-netty-server, 依赖spring-boot-parent
spring-bootNetty
Spring Boot: user notifications with web socket
This example will shows how to send notifications, via web socket, to specific logged-in users.
Could be useful, for example, if you are trying to implement a real-time user notification system.
Build
and run
springboot启动后: http://localhost/![](https://github.com/leelance/spring-boot-all/raw/master/spring-boot-websocket-netty-server/1.png)
![](https://github.com/leelance/spring-boot-all/raw/master/spring-boot-websocket-netty-server/2.png)
![](https://github.com/leelance/spring-boot-all/raw/master/spring-boot-websocket-netty-server/3.png)
![](https://github.com/leelance/spring-boot-all/raw/master/spring-boot-websocket-netty-server/4.png)
Configurations
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.12.Final</version> </dependency>
@SpringBootApplication public class NettyWebSocketServerApplication implements CommandLineRunner{ @Autowired private ChatServer chatServer; public static void main(String[] args) { SpringApplication.run(NettyWebSocketServerApplication.class, args); } @Bean public ChatServer chatServer() { return new ChatServer(); } @Override public void run(String... args) throws Exception { InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9090); ChannelFuture future = chatServer.start(address); Runtime.getRuntime().addShutdownHook(new Thread(){ @Override public void run() { chatServer.destroy(); } }); future.channel().closeFuture().syncUninterruptibly(); } }
@Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //处理日志 pipeline.addLast(new LoggingHandler(LogLevel.INFO)); //处理心跳 pipeline.addLast(new IdleStateHandler(0, 0, 1800, TimeUnit.SECONDS)); pipeline.addLast(new ChatHeartbeatHandler()); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpObjectAggregator(64 * 1024)); pipeline.addLast(new HttpRequestHandler("/ws")); pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); pipeline.addLast(new TextWebSocketFrameHandler(group)); }
Prerequisites
Java 8Maven > 3.0
From
terminal
Go on the project's root folder, then type:$ mvn spring-boot:run
项目完整路径:Spring-boot-netty-websocket
相关文章推荐
- springboot 集成 websocket
- SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送
- spring boot 2.0,netty,mybatis,mysql,redis,docker 集成
- SpringBoot 集成WebSocket广播模式
- SpringBoot集成WebSocket【基于STOMP协议】进行点对点[一对一]和广播[一对多]实时推送
- spring boot 集成websocket实现实时推送
- spring boot 集成原生netty(非netty-io-socket)
- springboot与thrift集成实现服务端和客户端
- spring boot 1.5.4 集成shiro+cas,实现单点登录和权限控制
- Spring Boot 集成MyBatis
- Spring Boot 集成Shiro和CAS
- WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探
- java开发之netty里集成spring注入mysq连接池(一)
- Spring Boot 集成Swagger
- 成功搭建CXFWebService服务端(集成spring)
- spring boot 集成框架事例
- spring boot 集成框架事例
- 集成jcaptcha到spring-boot+maven项目中时出现java.lang.NoSuchMethodError: com.jhlabs.image.WaterFilter的问题的解决方法