diff --git a/backend/pom.xml b/backend/pom.xml index 202c31d..c361fa1 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -60,6 +60,10 @@ org.springframework.boot spring-boot-starter-cache + + org.springframework.boot + spring-boot-starter-websocket + com.fasterxml.jackson.module jackson-module-kotlin diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt index 8bd1f42..5141651 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt @@ -9,12 +9,19 @@ import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import org.springframework.messaging.simp.SimpMessagingTemplate +import org.springframework.web.bind.annotation.CrossOrigin +import java.net.InetAddress import javax.validation.Valid @RestController @RequestMapping("/api") +@CrossOrigin class Controller { + @Autowired + lateinit var websocketMessenger: SimpMessagingTemplate + @Autowired lateinit var siteRepository: SiteRepository @Autowired @@ -104,6 +111,7 @@ class Controller { it.observations.add(observation) tutorRepository.save(it) } + sendObservationToSocket(observation) return observation.id } @@ -223,6 +231,15 @@ class Controller { } return AfiPieChart(AfiPieChartDataset(monitoring, knowledge, control, conservatism, teamwork)) } + + fun sendObservationToSocket(observation: Observation) { + websocketMessenger.convertAndSend("/ws/observations", observation) + } + + @GetMapping("/address") + fun getConnectionDetails(): Map { + return mapOf("ip" to InetAddress.getLocalHost().hostAddress) + } } data class AverageData( diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/WebSocketConfig.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/WebSocketConfig.kt new file mode 100644 index 0000000..96b53ea --- /dev/null +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/WebSocketConfig.kt @@ -0,0 +1,20 @@ +package uk.co.neviyn.observationdatabase + +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 +class WebSocketConfig : WebSocketMessageBrokerConfigurer { + + override fun configureMessageBroker(registry: MessageBrokerRegistry) { + registry.enableSimpleBroker("/ws") + } + + override fun registerStompEndpoints(registry: StompEndpointRegistry) { + registry.addEndpoint("/websocket").withSockJS() + } +} \ No newline at end of file