spring 如何存储WebSocket的“第一条消息”(使用java)

yftpprvb  于 2022-10-31  发布在  Spring
关注(0)|答案(1)|浏览(216)

我做了一个警报函数,它从WebSocket获取价格数据,如果条件满足就发送警报。我现在只有“价格条件”,我想添加“百分比条件”。所以我想做的是
1.打开WebSocket
1.取得第一资料,计算限价
1.例如)用户希望在价格下降5%时得到提醒。
1.打开WebSocket,当前价格是100$
1.因此,我需要在价格达到95美元(限制价格)时向用户发送警报
要做到这一点,我需要计算限价时,打开WebSocket。但我不知道“在哪里和如何”,我可以存储限价。
这是我的WebSocket代码实现价格条件(而不是“百分比条件”)

Web套接字客户端端点

  1. @ClientEndpoint
  2. public class WebsocketClientEndpoint {
  3. Session userSession = null;
  4. private MessageHandler messageHandler;
  5. public WebsocketClientEndpoint() {
  6. }
  7. public Session connect(URI endpointURI) {
  8. try {
  9. WebSocketContainer container = ContainerProvider.getWebSocketContainer();
  10. userSession = container.connectToServer(this, endpointURI);
  11. return userSession;
  12. } catch (Exception e) {
  13. throw new RuntimeException(e);
  14. }
  15. }
  16. /**
  17. * Callback hook for Connection open events.
  18. *
  19. * @param userSession the userSession which is opened.
  20. */
  21. @OnOpen
  22. public void onOpen(Session userSession) {
  23. System.out.println("opening websocket");
  24. this.userSession = userSession;
  25. }
  26. /**
  27. * Callback hook for Connection close events.
  28. *
  29. * @param userSession the userSession which is getting closed.
  30. * @param reason the reason for connection close
  31. */
  32. @OnClose
  33. public void onClose(Session userSession, CloseReason reason) {
  34. System.out.println("closing websocket");
  35. this.userSession = null;
  36. }
  37. /**
  38. * Callback hook for Message Events. This method will be invoked when a client send a message.
  39. *
  40. * @param message The text message
  41. */
  42. @OnMessage
  43. public void onMessage(String message) throws ParseException, IOException {
  44. if (this.messageHandler != null) {
  45. this.messageHandler.handleMessage(message);
  46. }
  47. }
  48. @OnMessage
  49. public void onMessage(ByteBuffer bytes) {
  50. System.out.println("Handle byte buffer");
  51. }
  52. /**
  53. * register message handler
  54. *
  55. * @param msgHandler
  56. */
  57. public void addMessageHandler(MessageHandler msgHandler) {
  58. this.messageHandler = msgHandler;
  59. }
  60. /**
  61. * Send a message.
  62. *
  63. * @param message
  64. */
  65. public void sendMessage(String message) {
  66. this.userSession.getAsyncRemote().sendText(message);
  67. }
  68. /**
  69. * Message handler.
  70. *
  71. * @author Jiji_Sasidharan
  72. */
  73. public static interface MessageHandler {
  74. public void handleMessage(String message) throws ParseException, IOException;
  75. }
  76. }

按价格提醒用户

  1. public void AlertUserByPrice(Long id) {
  2. Alert alert = alertRepository.findById(id).orElseThrow(() -> new NoSuchElementException());
  3. String type = alert.getAlertType().getKey();
  4. double SetPrice = alert.getPrice();
  5. String ticker = alert.getTicker();
  6. JSONParser jsonParser = new JSONParser();
  7. final NotificationRequest build;
  8. if (type == "l_break") {
  9. build = NotificationRequest.builder()
  10. .title(ticker + " alert")
  11. .message(SetPrice + "broke down")
  12. .token(notificationService.getToken(userDetailService.returnUser().getEmail()))
  13. .build();
  14. }
  15. else { // upper_break
  16. build = NotificationRequest.builder()
  17. .title(ticker + " alert")
  18. .message(SetPrice + "pierced upward")
  19. .token(notificationService.getToken(userDetailService.returnUser().getEmail()))
  20. .build();
  21. }
  22. try {
  23. final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint();
  24. Session session = clientEndPoint.connect(new URI("wss://ws.coincap.io/prices?assets=" + ticker));
  25. WebsocketClientEndpoint.MessageHandler handler = new WebsocketClientEndpoint.MessageHandler() {
  26. public void handleMessage(String message) throws ParseException, IOException {
  27. Object obj = jsonParser.parse(message);
  28. JSONObject jsonObject = (JSONObject) obj;
  29. double price = Double.parseDouble(jsonObject.get(ticker).toString());
  30. System.out.println("가격 : " + price);
  31. if (type == "l_break") {
  32. if (price < SetPrice) {
  33. System.out.println("끝");
  34. notificationService.sendNotification(build);
  35. session.close();
  36. }
  37. } else {
  38. if (price > SetPrice) {
  39. System.out.println("끝");
  40. notificationService.sendNotification(build);
  41. session.close();
  42. }
  43. }
  44. try {
  45. Thread.sleep(1000);
  46. } catch (InterruptedException ex) {
  47. System.err.println("InterruptedException exception: " + ex.getMessage());
  48. }
  49. }
  50. };
  51. clientEndPoint.addMessageHandler(handler);
  52. } catch (URISyntaxException ex) {
  53. System.err.println("URISyntaxException exception: " + ex.getMessage());
  54. }
  55. }

我该怎么做才能实现“按血统条件报警”??请有人帮忙..提前感谢

zte4gxcn

zte4gxcn1#

  1. double percent = 0.05;
  2. if (price-(price*percent) < SetPrice) {
  3. System.out.println("끝");
  4. notificationService.sendNotification(build);
  5. session.close();
  6. }
  7. if (price-(price*percent) > SetPrice) {
  8. System.out.println("끝");
  9. notificationService.sendNotification(build);
  10. session.close();
  11. }

如果有帮助,请尝试

相关问题