如下是为了实现:业务数据实时展现,自动更新
前端
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
复制代码
WebSocketConfigjava
/** * 开启WebSocket支持 * @author zhengkai */
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
复制代码
@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketServer {
private final Logger log = LoggerFactory.getLogger(this.getClass());
//concurrent包的线程安全Set,用来存放每一个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
//与某个客户端的链接会话,须要经过它来给客户端发送数据
private Session session;
/** * 链接创建成功调用的方法 */
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this);
sendMessage("链接成功");
}
/** * 链接关闭调用的方法 */
@OnClose
public void onClose() {
webSocketSet.remove(this);
log.info("有一链接关闭!");
}
/** * 收到客户端消息后调用的方法 * * @param queryType 客户端发送过来的消息 */
@OnMessage
public void onMessage(String queryType) {
log.info("收到信息:" + queryType);
//群发消息
for (WebSocketServer item : webSocketSet) {
item.sendMessage("hello");
}
}
/** * @param session * @param error */
@OnError
public void onError(Session session, Throwable error) {
log.error("发生错误");
error.printStackTrace();
}
/** * 实现服务器主动推送 */
public void sendMessage(Object message) {
try {
this.session.getBasicRemote().sendText(JSON.toJSONString(message));
} catch (IOException e) {
e.printStackTrace();
}
}
/** * 实现服务器主动群发消息 */
public static void sendInfo(Object message) {
for (WebSocketServer item : webSocketSet) {
item.sendMessage(message);
}
}
}
复制代码
<script>
var socket;
if(typeof(WebSocket) == "undefined") {
console.log("您的浏览器不支持WebSocket");
}else{
console.log("您的浏览器支持WebSocket");
//实现化WebSocket对象,指定要链接的服务器地址与端口 创建链接
socket = new WebSocket("ws://localhost:8080/webscoket");
//打开事件
socket.onopen = function() {
console.log("Socket 已打开");
//socket.send("这是来自客户端的消息" + location.href + new Date());
};
//得到消息事件
socket.onmessage = function(msg) {
console.log(msg.data);
//发现消息进入 开始处理前端触发逻辑
};
//关闭事件
socket.onclose = function() {
console.log("Socket已关闭");
};
//发生了错误事件
socket.onerror = function() {
alert("Socket发生了错误");
//此时能够尝试刷新页面
}
}
</script>
复制代码
// spring security
.antMatchers(
"/websocket"
)
.permitAll()
复制代码
@ServerEndpoint(value = "/websocket")
对应的地址是:ws://localhost:8080/webscoket
复制代码
@Pointcut("execution(public * com.xxx.xxx.controller..*(..))")
public void webLog() {
}
复制代码
解决办法web
// 排除拦截
@Pointcut("execution(public * com.xxx.xxx.controller..*(..)) && !execution(public * com.xxx.xxx.controller.xxx*(..))")
public void webLog() {
}
复制代码
即你按以下方式直接使用@Autowired,会报错spring
@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketServer {
@Autowired
ChartDataService chartDataService;
}
复制代码
在WebSocket中, 因 SpringBoot+WebSocket 对每一个客户端链接都会建立一个 WebSocketServer(@ServerEndpoint 注解对应的) 对象,Bean 注入操做会被直接略过,于是手动注入一个全局变量(即你须要注入的bean)浏览器
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
// 在这里注入你须要的bean
@Autowired
public void setMessageService(ChartDataService chartDataService) {
WebSocketServer.chartDataService = chartDataService;
// ...
}
}
复制代码
使用你注入的bean安全
@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketServer {
public static ChartDataService chartDataService;
复制代码
此方式经实践,有效bash
其余解决方式: stackoverflow.com/questions/2…服务器
@ServerEndpoint("/ws")
public class MyWebSocket {
@Inject
private ObjectMapper objectMapper;
}
复制代码
@ServerEndpoint(value = "/ws", configurator = SpringConfigurator.class)
复制代码
@PostConstruct
public void init(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
复制代码
通过实践,上述三种方式,对于我而言,均无效websocket
参考连接:blog.csdn.net/moshowgame/…session