You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package com.dreamworks.boditech.service;
  2. import com.dreamworks.boditech.utils.MyWsServerConnection;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import jakarta.annotation.PostConstruct;
  6. import org.springframework.stereotype.Service;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @Service
  10. public class WebsocketServerService {
  11. // connections
  12. private List<MyWsServerConnection> connections;
  13. @PostConstruct
  14. public void init() {
  15. this.connections = new ArrayList<>();
  16. }
  17. public void appendConnection(MyWsServerConnection connection) {
  18. this.connections.add(connection);
  19. }
  20. // send message to client
  21. public void send(String message) {
  22. for (MyWsServerConnection connection : this.connections) {
  23. connection.send(message);
  24. }
  25. }
  26. // send object to client
  27. public void send( Object object ) {
  28. ObjectMapper mapper = new ObjectMapper();
  29. String json = null;
  30. try {
  31. json = mapper.writeValueAsString(object);
  32. } catch (JsonProcessingException e) {
  33. throw new RuntimeException(e);
  34. }
  35. this.send(json);
  36. }
  37. }