新增websocket配置

pull/1/head
xiaoCJ 2 years ago
parent ab7aa2c219
commit dda1971f80

@ -1,24 +0,0 @@
package com.sztzjy.forex.trading_trading.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
// 配置消息代理
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").withSockJS(); // 配置 WebSocket 端点
}
}

@ -80,6 +80,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/v2/**").permitAll()
.antMatchers("/test/**").permitAll()
.antMatchers("/druid/**").permitAll()
.antMatchers("/webSocket/**").permitAll()
.antMatchers(anonymousUrls.toArray(new String[]{})).permitAll()
.anyRequest().authenticated();
}

@ -0,0 +1,53 @@
package com.sztzjy.forex.trading_trading.config.websocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ServerEndpoint("/webSocket/{username}")
@Slf4j
@Component
public class WebSocket {
private static int onlineCount = 0;
private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
private Session session;
private String username;
@OnOpen
public void onOpen(@PathParam("username") String username, Session session) {
this.username = username;
this.session = session;
WebSocket.onlineCount++;
clients.put(username, this);
}
@OnClose
public void onClose() {
clients.remove(username);
WebSocket.onlineCount--;
}
@OnMessage
public void onMessage(String message) {}
@OnError
public void onError(Session session, Throwable throwable) {
log.error("WebSocket发生错误" + throwable.getMessage());
}
public static void sendMessage(String message) {
// 向所有连接websocket的客户端发送消息
// 可以修改为对某个客户端发消息
for (WebSocket item : clients.values()) {
item.session.getAsyncRemote().sendText(message);
}
}
}

@ -0,0 +1,18 @@
package com.sztzjy.forex.trading_trading.config.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
@EnableWebSocket
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

@ -3,15 +3,13 @@ package com.sztzjy.forex.trading_trading.service;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sztzjy.forex.trading_trading.config.websocket.WebSocket;
import com.sztzjy.forex.trading_trading.entity.*;
import com.sztzjy.forex.trading_trading.mappers.*;
import com.sztzjy.forex.trading_trading.util.ForexMarketDateUtil;
import com.sztzjy.forex.trading_trading.util.RedisUtil;
import com.sztzjy.forex.trading_trading.util.file.IFileUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@ -29,8 +27,6 @@ public class WainingService {
@Autowired
private MarketWarningMapper marketWarningMapper;
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@Autowired
private MarketWarningRecordMapper marketWarningRecordMapper;
@Autowired
private ForexMarketDateUtil forexMarketDateUtil;
@ -46,6 +42,8 @@ public class WainingService {
private TransactionNumberWarningMapper transactionNumberWarningMapper;
@Autowired
private TransactionNumberWarningRecordMapper transactionNumberWarningRecordMapper;
@Autowired
private WebSocket webSocket;
//保证金比较接口
public String compareMarginLevels(String memberId, String trainingId) {
@ -70,7 +68,7 @@ public class WainingService {
marginWarningRecord.setWarningTime(current);
marginWarningRecord.setEarlyWarningLevel(memberLevel);
marginWarningRecordMapper.updateByPrimaryKeySelective(marginWarningRecord);
simpMessagingTemplate.convertAndSend("/topic/margin-change", message);
WebSocket.sendMessage(message);
}
}
}
@ -101,6 +99,8 @@ public class WainingService {
BeanUtils.copyProperties(member, marginWarning);
marginWarning.setStatus(0);
marginWarning.setId(IdUtil.simpleUUID());
marginWarning.setMarginLevel(50.12);
marginWarning.setNetValue(50.2131);
marginWarningMapper.insert(marginWarning);
}
return marginWarning;
@ -195,7 +195,7 @@ public class WainingService {
if (direction.equals("买")) {
// 3.拿redis的价位和新增的高价低价做比较==的时候就弹出提示,并且存入行情预警记录表
if (buyPic.equals(upperBreakPrice) || buyPic.equals(lowerBreakPrice)) {
simpMessagingTemplate.convertAndSend("/topic/alerts", message);
WebSocket.sendMessage(message);
marketWarningRecord.setWarningPrice(buyPic);
marketWarningRecord.setWarningTime((new Timestamp(new Date().getTime())));
marketWarningRecordMapper.updateByPrimaryKeySelective(marketWarningRecord);
@ -203,7 +203,7 @@ public class WainingService {
}
} else {
if (sellPicValue.equals(upperBreakPrice) || sellPicValue.equals(lowerBreakPrice)) {
simpMessagingTemplate.convertAndSend("/topic/alerts", message);
WebSocket.sendMessage(message);
marketWarningRecord.setWarningPrice(sellPicValue);
marketWarningRecord.setWarningTime((new Timestamp(new Date().getTime())));
marketWarningRecordMapper.updateByPrimaryKeySelective(marketWarningRecord);
@ -319,6 +319,7 @@ public class WainingService {
// 如果当前交易次数在到达用户设置的预警时间时还是小于,那么就返回消息给前端提示
tnwr.setWarningingCount(openingTrades);
tnwr.setWarningTime(new Timestamp(new Date().getTime()));
WebSocket.sendMessage(message);
transactionNumberWarningRecordMapper.updateByPrimaryKeySelective(tnwr);
}
}

Loading…
Cancel
Save