diff --git a/socket-service/socket-service.md b/socket-service/socket-service.md new file mode 100644 index 0000000..f77921a --- /dev/null +++ b/socket-service/socket-service.md @@ -0,0 +1,15 @@ +# socket-service: is a microservice that consists of several classes related to a socket service in a Java Spring Boot application. Here's a brief overview of each class: + +1. `SocketServiceApplication` : This class serves as the entry point of the application. It starts the Spring Boot application by calling `SpringApplication.run()` . + +2. `SocketServiceConfig` : This class is a configuration class annotated with `@Configuration` and `@EnableWebSocketMessageBroker` . It configures the message broker and WebSocket endpoints using the `WebSocketMessageBrokerConfigurer` interface. point your browser to "http://localhost:8080/profile" after running the "|>" button. + +3. `SocketServiceController` : This class is annotated with `@Controller` and handles WebSocket messages. It has a `handleMessage()` method that receives messages sent to the "/message" destination and sends the processed message to the "/topic/messages" destination. The `SocketServiceController` also uses a `SocketServiceRepository` to save the received messages. + +4. `SocketServiceRepository` : This class is annotated with `@Repository` and defines the interface for the repository that handles data storage. It does not contain any method declarations in the provided code. + +These classes work together to create a socket service that handles incoming messages, processes them, and saves them using a repository. + +The js files in `socket-service/src/main/resources/static` directory are just samples. Replace 'ws://your-websocket-server-url' with the actual WebSocket server URL that you want to connect to. + +This JavaScript code establishes a WebSocket connection, listens for incoming messages, and sends messages to the server using the socket.send() method. \ No newline at end of file diff --git a/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceApplication.java b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceApplication.java new file mode 100644 index 0000000..48296c4 --- /dev/null +++ b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceApplication.java @@ -0,0 +1,16 @@ +package main.java.com.safeforum.socket_service; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SocketServiceApplication { + + public static void main(String[] args) { + try { + SpringApplication.run(SocketServiceApplication.class, args); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceConfig.java b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceConfig.java new file mode 100644 index 0000000..4b91aa4 --- /dev/null +++ b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceConfig.java @@ -0,0 +1,37 @@ +package main.java.com.safeforum.socket_service; + +import org.springframework.context.annotation.Bean; +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 +public class SocketServiceConfig implements WebSocketMessageBrokerConfigurer { + + @Bean + public SocketServiceRepository socketServiceRepository() { + return new SocketServiceRepository() { + /** + * @param message + */ + @Override + public void saveMessage(String message) { + + } + }; + } + + @Override + public void configureMessageBroker(MessageBrokerRegistry config) { + config.enableSimpleBroker("/topic"); // Enable a simple message broker + config.setApplicationDestinationPrefixes("/app"); // Prefix for application-level messages + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/socket").withSockJS(); // Register the WebSocket endpoint + } +} \ No newline at end of file diff --git a/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceController.java b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceController.java new file mode 100644 index 0000000..12c8dd1 --- /dev/null +++ b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceController.java @@ -0,0 +1,25 @@ +package main.java.com.safeforum.socket_service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; + +@Controller +public class SocketServiceController { + + @Autowired + private SocketServiceRepository repository; + + public SocketServiceController(SocketServiceRepository repository) { + this.repository = repository; + } + + @MessageMapping("/message") + @SendTo("/topic/messages") + public String handleMessage(String message) { + // Handle the incoming message + repository.saveMessage(message); + return "Received message: " + message; + } +} diff --git a/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceRepository.java b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceRepository.java new file mode 100644 index 0000000..1718b35 --- /dev/null +++ b/socket-service/src/main/java/com/safeforum/socket_service/SocketServiceRepository.java @@ -0,0 +1,9 @@ +package main.java.com.safeforum.socket_service; + +import org.springframework.stereotype.Repository; + +@Repository +public interface SocketServiceRepository { + + void saveMessage(String message); +} diff --git a/socket-service/src/main/resources/static/SocketServiceApplication.js b/socket-service/src/main/resources/static/SocketServiceApplication.js new file mode 100644 index 0000000..ae80005 --- /dev/null +++ b/socket-service/src/main/resources/static/SocketServiceApplication.js @@ -0,0 +1,29 @@ +// SocketServiceApplication.js +const express = require('express'); +const app = express(); + +app.use(express.json()); + +app.listen(3000, () => { + console.log('Socket service application is running on port 3000'); +}); + +// SocketServiceConfig.js +const WebSocket = require('ws'); + +const wss = new WebSocket.Server({ port: 8080 }); + +wss.on('connection', (ws) => { + ws.on('message', (message) => { + handleMessage(message); + }); +}); + +// SocketServiceController.js +function handleMessage(message) { + console.log('Received message:', message); + // Add your logic to handle the message and save it to the repository +} + +// SocketServiceRepository.js +// Implement your repository logic here for saving the messages \ No newline at end of file diff --git a/socket-service/src/main/resources/static/websocketclient.js b/socket-service/src/main/resources/static/websocketclient.js new file mode 100644 index 0000000..9b70742 --- /dev/null +++ b/socket-service/src/main/resources/static/websocketclient.js @@ -0,0 +1,18 @@ +const socket = new WebSocket('ws://your-websocket-server"); + +// Connection opened +socket.addEventListener('open', (event) => { + console.log('WebSocket connection established'); +}); + +// Listen for messages +socket.addEventListener('message', (event) => { + const message = event.data; + console.log('Received message:', message); + // Handle the received message as needed +}); + +// Send a message +function sendMessage(message) { + socket.send(message); +} \ No newline at end of file diff --git a/socket-service/src/main/resources/templates/application.yaml b/socket-service/src/main/resources/templates/application.yaml new file mode 100644 index 0000000..cf85f1e --- /dev/null +++ b/socket-service/src/main/resources/templates/application.yaml @@ -0,0 +1,4 @@ +spring: + cloud: + function: + definition: persistentEntities \ No newline at end of file diff --git a/src/main/java/com/mysafeforum/DemoApplication.java b/src/main/java/com/mysafeforum/DemoApplication.java deleted file mode 100644 index f3258e0..0000000 --- a/src/main/java/com/mysafeforum/DemoApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mysafeforum; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class DemoApplication { - - public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); - } - -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/test/java/com/mysafeforum/TestDemoApplication.java b/src/test/java/com/mysafeforum/TestDemoApplication.java index 553ad94..a0064f6 100644 --- a/src/test/java/com/mysafeforum/TestDemoApplication.java +++ b/src/test/java/com/mysafeforum/TestDemoApplication.java @@ -1,23 +1,24 @@ -package com.mysafeforum; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.testcontainers.service.connection.ServiceConnection; -import org.springframework.context.annotation.Bean; -import org.testcontainers.containers.MongoDBContainer; -import org.testcontainers.utility.DockerImageName; - -@TestConfiguration(proxyBeanMethods = false) -public class TestDemoApplication { - - @Bean - @ServiceConnection - MongoDBContainer mongoDbContainer() { - return new MongoDBContainer(DockerImageName.parse("mongo:latest")); - } - - public static void main(String[] args) { - SpringApplication.from(DemoApplication::main).with(TestDemoApplication.class).run(args); - } - -} +//package com.mysafeforum; +// +//import main.java.com.safeforum.socket_service.DemoApplication; +//import org.springframework.boot.SpringApplication; +//import org.springframework.boot.test.context.TestConfiguration; +//import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +//import org.springframework.context.annotation.Bean; +//import org.testcontainers.containers.MongoDBContainer; +//import org.testcontainers.utility.DockerImageName; +// +//@TestConfiguration(proxyBeanMethods = false) +//public class TestDemoApplication { +// +// @Bean +// @ServiceConnection +// MongoDBContainer mongoDbContainer() { +// return new MongoDBContainer(DockerImageName.parse("mongo:latest")); +// } +// +// public static void main(String[] args) { +// SpringApplication.from(DemoApplication::main).with(TestDemoApplication.class).run(args); +// } +// +//}