Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions socket-service/socket-service.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main.java.com.safeforum.socket_service;

import org.springframework.stereotype.Repository;

@Repository
public interface SocketServiceRepository {

void saveMessage(String message);
}
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions socket-service/src/main/resources/static/websocketclient.js
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 4 additions & 0 deletions socket-service/src/main/resources/templates/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring:
cloud:
function:
definition: persistentEntities
13 changes: 0 additions & 13 deletions src/main/java/com/mysafeforum/DemoApplication.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

47 changes: 24 additions & 23 deletions src/test/java/com/mysafeforum/TestDemoApplication.java
Original file line number Diff line number Diff line change
@@ -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);
// }
//
//}