Chat com spribgboot e angular 10

Estou precisando de fazer um chat. Andei pesquisando mas não consegui entender muito.

Configurei assim no pom

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-websocket</artifactId>
		</dependency>

Configuration

package br.com.ghnetsoft.apifinanceiro.configuracao.config;

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 WebSocketConfig implements WebSocketMessageBrokerConfigurer {

	@Override
	public void configureMessageBroker(MessageBrokerRegistry config) {
		config.enableSimpleBroker("/topic");
		config.setApplicationDestinationPrefixes("/app");
	}

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
	}
}

E assim no controller

package br.com.ghnetsoft.apifinanceiro.configuracao.resource;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import br.com.ghnetsoft.apifinanceiro.configuracao.dto.Greeting;
import br.com.ghnetsoft.apifinanceiro.configuracao.dto.HelloMessage;
import lombok.extern.apachecommons.CommonsLog;

@CommonsLog
@RestController
public class GreetingController {

	@MessageMapping("/mensagem")
	@SendTo("/envio")
	public Greeting greeting(HelloMessage message) throws Exception {
		Thread.sleep(1000); // simulated delay
		log.info("teste");
		return new Greeting("Olá, " + HtmlUtils.htmlEscape(message.getName()) + "!");
	}
}

no log, só aconteceu isso

2021-04-14 21:42:10.106  INFO 37380 --- [  restartedMain] o.s.m.s.b.SimpleBrokerMessageHandler     : Starting...
2021-04-14 21:42:10.107  INFO 37380 --- [  restartedMain] o.s.m.s.b.SimpleBrokerMessageHandler     : BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
2021-04-14 21:42:10.107  INFO 37380 --- [  restartedMain] o.s.m.s.b.SimpleBrokerMessageHandler     : Started.
2021-04-14 21:42:10.119  INFO 37380 --- [  restartedMain] b.c.g.a.c.ConfiguracaoApiApplication     : Started ConfiguracaoApiApplication in 7.56 seconds (JVM running for 8.863)
2021-04-14 21:43:08.620  INFO 37380 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats    : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]

Esta certo ?

No angular ainda nem começei.

Alguém ?

Qual é o problema? Tenta pelo menos testar.

1 curtida

Ele não deveria imprimir o teste ?

Se sim não imprimi. Por isso abri um tópico.

Nao achei no que você postou algum client se conectando ao que foi mapeando em @MessageMapping("/mensagem")

1 curtida

Nao sei qual material de estudo está seguindo. Mas aqui tem um exemplo:

1 curtida

Não fiz nada no cliente. Eu só fiz, e dei start no server. Mas como tem o este item

Thread.sleep(1000); // simulated delay
log.info("teste");

E pelo que eu li, ele deveria fazer um thread com este tempo

entende ?

Vou ver aqui. Então pelo que entendi, não basta só ter o back end, isto é, dar um start no server, tem que ter o front, com websocket chamando este mapeamento, correto ?