spring@messagemapping controller不会通过套接字从服务器接收消息

b4qexyjb  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(260)

它具有jwt身份验证。当我通过套接字发送消息时,我传递一个带有查询参数的身份验证令牌。根据spring logger,这个请求是正确的身份验证,尽管它不能到达控制器。
web套接字配置代码

@Configuration
@EnableWebSocketMessageBroker
@CrossOrigin
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

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

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

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
        resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setObjectMapper(new ObjectMapper());
        converter.setContentTypeResolver(resolver);
        messageConverters.add(converter);
        return false;
    }

}

web套接字控制器

@Controller
@CrossOrigin(origins = "http://localhost:4200")
public class GreetingController {

    @Autowired private SimpMessagingTemplate messagingTemplate;

    @MessageMapping("/chat")
    public void processMessage(@Payload Message chatMessage) {
        System.out.println("HELLO");
        Notification notification = new Notification();
        notification.setSenderId(chatMessage.getSenderId());
        notification.setSenderName(chatMessage.getSenderName());
        messagingTemplate.convertAndSendToUser(
                chatMessage.getRecipientId(),"/queue/messages",
                notification);
    }

}

web套接字服务文件

import { Injectable, OnDestroy } from "@angular/core";
import * as Stomp from 'stompjs';
import * as Socket from 'socket.io-client';
import { AuthService } from "../auth/auth.service";
import { User } from "../auth/user.model";
import { Subscription } from "rxjs";
import * as SockJS from "sockjs-client";

@Injectable({providedIn:'root'})
export class MessageService implements OnDestroy{

    currentUser:User;
    userSubscription:Subscription;
    constructor(private authService:AuthService) {

      }

      public stompClient;
      public msg = [];
      initializeWebSocketConnection() {
        this.userSubscription = this.authService.user.subscribe(
            user=>{
                this.currentUser = user;
            }
        );
        const serverUrl = 'http://localhost:8080/ws?param='.concat(this.currentUser.token);
        const ws = new Socket(serverUrl);

        this.stompClient = Stomp.over(ws);
        const that = this;
        // tslint:disable-next-line:only-arrow-functions
        this.stompClient.connect({},this.onConnected());
      }

      onConnected(){
        console.log("CONNECTED");
        this.stompClient.subscribe(
            "/user/"+14+"/queue/messages",
            message => {
                console.log("RECEIVED");
                console.log(message);
            }
        )
      }

      sendMessage() {
        console.log("MESSAGE SENT");
        const message = {
            senderId: 14,
            recipientId: 14,
            senderName: "Lecturer",
            recipientName: "Kiryl",
            content: "THIS IS A MESSAGE TO EVERY",
            timestamp: new Date(),
        }
        console.log(JSON.stringify(message));
        this.stompClient.send('/app/chat' , {}, JSON.stringify(message));
      }

      ngOnDestroy(): void {
            this.userSubscription.unsubscribe();
        }
}

这就是我在控制台里看到的

有人知道怎么解决吗???

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题