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
41 lines
1.2 KiB
package com.dreamworks.boditech.service;
|
|
import com.dreamworks.boditech.utils.MyWsServerConnection;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import jakarta.annotation.PostConstruct;
|
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
@Service
|
|
public class WebsocketServerService {
|
|
// connections
|
|
private List<MyWsServerConnection> connections;
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
this.connections = new ArrayList<>();
|
|
}
|
|
|
|
public void appendConnection(MyWsServerConnection connection) {
|
|
this.connections.add(connection);
|
|
}
|
|
|
|
// send message to client
|
|
public void send(String message) {
|
|
for (MyWsServerConnection connection : this.connections) {
|
|
connection.send(message);
|
|
}
|
|
}
|
|
|
|
// send object to client
|
|
public void send( Object object ) {
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
String json = null;
|
|
try {
|
|
json = mapper.writeValueAsString(object);
|
|
} catch (JsonProcessingException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
this.send(json);
|
|
}
|
|
}
|