Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import grails.transaction.Transactional
- import org.springframework.messaging.simp.SimpMessageSendingOperations
- class MyController {
- MyService myService
- SimpMessageSendingOperations messagingTemplate
- @Transactional
- def addItems() {
- def items = request.JSON.items
- myService.addItemsToList(items) { progress ->
- messagingTemplate.convertAndSend("/topic/progress", [percent: progress])
- }
- }
- }
- ------------------
- import org.springframework.stereotype.Service
- @Service
- class MyService {
- List<String> itemsList = []
- def addItemsToList(List<String> items, Closure progressCallback) {
- items.eachWithIndex { item, i ->
- itemsList << item
- progressCallback(100.0 * (i + 1) / items.size())
- }
- }
- }
- -----------------------------------------
- import grails.transaction.Transactional
- import org.springframework.messaging.simp.SimpMessageSendingOperations
- class MyController {
- MyService myService
- SimpMessageSendingOperations messagingTemplate
- @Transactional
- def addItems() {
- def items = request.JSON.items
- myService.itemsList = []
- myService.addItemsToList(items)
- }
- }
- --------------
- import org.springframework.stereotype.Service
- @Service
- class MyService {
- List<String> itemsList = []
- SimpMessageSendingOperations messagingTemplate
- def addItemsToList(List<String> items) {
- items.eachWithIndex { item, i ->
- itemsList << item
- messagingTemplate.convertAndSend("/topic/progress", [percent: (100.0 * (i + 1) / items.size())])
- }
- }
- }
- -----------
- FE
- // Create a WebSocket connection to the server
- const socket = new SockJS('/ws');
- const stompClient = Stomp.over(socket);
- // Connect to the server
- stompClient.connect({}, function () {
- // Subscribe to the "/topic/progress" topic
- stompClient.subscribe('/topic/progress', function (message) {
- // Parse the progress information from the message
- const progress = JSON.parse(message.body);
- console.log(`Progress: ${progress.percent}%`);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment