2019独角兽企业重金招聘Python工程师标准>>>
spring-boot-websocket-netty-server, 依赖spring-boot-parent
- spring-boot
- Netty
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/
Configurations
1
2
3
4
5
6 1<dependency>
2 <groupId>io.netty</groupId>
3 <artifactId>netty-all</artifactId>
4 <version>4.1.12.Final</version>
5</dependency>
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1@SpringBootApplication
2public class NettyWebSocketServerApplication implements CommandLineRunner{
3 @Autowired
4 private ChatServer chatServer;
5
6 public static void main(String[] args) {
7 SpringApplication.run(NettyWebSocketServerApplication.class, args);
8 }
9
10 @Bean
11 public ChatServer chatServer() {
12 return new ChatServer();
13 }
14
15 @Override
16 public void run(String... args) throws Exception {
17 InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9090);
18 ChannelFuture future = chatServer.start(address);
19
20 Runtime.getRuntime().addShutdownHook(new Thread(){
21 @Override
22 public void run() {
23 chatServer.destroy();
24 }
25 });
26
27 future.channel().closeFuture().syncUninterruptibly();
28 }
29}
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1public class ChatServerInitializer extends ChannelInitializer<Channel>{
2 private final ChannelGroup group;
3
4 public ChatServerInitializer(ChannelGroup group) {
5 this.group = group;
6 }
7
8 @Override
9 protected void initChannel(Channel ch) throws Exception {
10 ChannelPipeline pipeline = ch.pipeline();
11 //处理日志
12 pipeline.addLast(new LoggingHandler(LogLevel.INFO));
13
14 //处理心跳
15 pipeline.addLast(new IdleStateHandler(0, 0, 1800, TimeUnit.SECONDS));
16 pipeline.addLast(new ChatHeartbeatHandler());
17
18 pipeline.addLast(new HttpServerCodec());
19 pipeline.addLast(new ChunkedWriteHandler());
20 pipeline.addLast(new HttpObjectAggregator(64 * 1024));
21 pipeline.addLast(new HttpRequestHandler("/ws"));
22 pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
23 pipeline.addLast(new TextWebSocketFrameHandler(group));
24 }
25}
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 1private void offlines(ChannelHandlerContext ctx) {
2 Channel channel = ctx.channel();
3 String token = channel.attr(ChatConstants.CHANNEL_TOKEN_KEY).get();
4
5 UserInfo info = ChatConstants.onlines.get(token);
6 if(info == null) {
7 group.remove(channel);
8 ctx.close();
9 return;
10 }
11 ChatMessage message = new ChatMessage(info, "["+info.getCode()+"], 下线了.", ChatMessage.OFF);
12 group.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(message)));
13
14 ChatConstants.removeOnlines(token);
15 group.remove(channel);
16 ctx.close();
17 }
18
Prerequisites
- Java 8
- Maven > 3.0
From terminal
Go on the project's root folder, then type:
1
2 1$ mvn spring-boot:run
2
项目完整路径:spring-boot-netty