SpringBoot中建⽴WebSocket连接(STOMP实现发送消息给
指定⽤户)
⼗分感谢博主解决了我的⼈⽣⼤事啊!
使⽤STOMP实现发送消息给指定⽤户步骤如下:
1.添加pom⽂件依赖
<!-- springboot websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
1
2
3
4
5
2.书写客户端⽤户实体类
⾃定义客户端⽤户实体类,封装来⾃于客户端的信息,相当于为每⼀个客户端提供唯⼀的标识
package ity;
import java.security.Principal;
/
**
*
* @ClassName: User
* @Description: 客户端⽤户
* @author cheng
* @date 2017年9⽉29⽇下午3:02:54
*/
public final class User implements Principal {
private final String name;
public User(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
3.书写客户端渠道拦截适配器
利⽤拦截的⽅式,获取包含在stomp中的⽤户信息,并将认证的⽤户信息设置到当前的访问器中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16package com.ahut.websocket;
import java.util.LinkedList;
import java.util.Map;
import ssaging.Message;
import ssaging.MessageChannel;
import ssaging.simp.SimpMessageHeaderAccessor;
import ssaging.simp.stomp.StompCommand;
import ssaging.simp.stomp.StompHeaderAccessor;
import ssaging.support.ChannelInterceptorAdapter;
import ssaging.support.MessageHeaderAccessor;
import ity.User;
/**
*
* @ClassName: UserInterceptor
* @Description: 客户端渠道拦截适配器
* @author cheng
* @date 2017年9⽉29⽇下午2:40:12
*/
public class UserInterceptor extends ChannelInterceptorAdapter {
/**
* 获取包含在stomp中的⽤户信息
*/
@SuppressWarnings("rawtypes")
websocket和socket
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = Accessor(message, StompHeaderAccessor.class); if (StompCommand.CONNECT.Command())) {
Object raw = Headers().get(SimpMessageHeaderAccessor.NATIVE_HEADERS);
if (raw instanceof Map) {
Object name = ((Map) raw).get("name");
if (name instanceof LinkedList) {
// 设置当前访问器的认证⽤户
accessor.setUser(new User(((LinkedList) name).get(0).toString()));
}
}
}
return message;
}
}
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
4.配置websocket stomp
package fig;
import t.annotation.Bean;
import t.annotation.Configuration;
import fig.ChannelRegistration;
import fig.MessageBrokerRegistry;
import org.springframework.fig.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.fig.annotation.EnableWebSocketMessageBroker;
import org.springframework.fig.annotation.StompEndpointRegistry;
import com.ahut.websocket.UserInterceptor;
/**
*
* @ClassName: WebSocketStompConfig
* @Description: springboot websocket stomp配置
* @author cheng
* @date 2017年9⽉27⽇下午3:45:36
*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig extends AbstractWebSocketMessageBrokerConfigurer {
/**
* 注册stomp的端点
*/
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// 允许使⽤socketJs⽅式访问,访问点为webSocketServer,允许跨域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // 允许使⽤socketJs⽅式访问,访问点为webSocketServer,允许跨域
// 在⽹页上我们就可以通过这个链接
// localhost:8080/webSocketServer
// 来和服务器的WebSocket连接
registry.addEndpoint("/webSocketServer").setAllowedOrigins("*").withSockJS();
}
/**
* 配置信息代理
*/
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
// 订阅Broker名称
// 全局使⽤的消息前缀(客户端订阅路径上会体现出来)
registry.setApplicationDestinationPrefixes("/app");
// 点对点使⽤的订阅前缀(客户端订阅路径上会体现出来),不设置的话,默认也是/user/ // registry.setUserDestinationPrefix("/user/");
}
/**
* 配置客户端⼊站通道
*/
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(createUserInterceptor());
}
/**
*
* @Title: createUserInterceptor
* @Description: 将客户端渠道加⼊spring ioc容器
* @return
*/
@Bean
public UserInterceptor createUserInterceptor() {
return new UserInterceptor();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论