backend/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java

42 lines
1.4 KiB
Java

package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import javax.websocket.server.ServerEndpointConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServerEndpointRegistration;
@Configuration
public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
public static SensorSocketEndpoint getInstance() {
return instance;
}
private static SensorSocketEndpoint instance = new SensorSocketEndpoint();
@Bean
public ServerEndpointRegistration sensorSocketEndpoint() {
return new ServerEndpointRegistration("/sensor-socket", instance);
}
@Bean
public ServerEndpointExporter endpointExporter() {
return new ServerEndpointExporter();
}
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
try {
@SuppressWarnings("unchecked")
final T instance = (T) SensorSocketConfig.instance;
return instance;
} catch (ClassCastException e) {
final var e2 =
new InstantiationException("Cannot cast SensorSocketEndpoint to desired type");
e2.initCause(e);
throw e2;
}
}
}