Reputation: 13
I want to connect my Angular client to Spring Boot server via websockets(Sock.js and Stomp.js). But I receive this error in the browser:
My websocket config:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-endpoint").setAllowedOrigins("http://localhost:4200").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic"); // Enable a simple in-memory message broker
registry.setApplicationDestinationPrefixes("/app"); // Prefix for messages that are sent to controllers
}
}
Websocket client on Angular:
import { Injectable } from '@angular/core';
import {Client, Message} from "@stomp/stompjs";
@Injectable({
providedIn: 'root'
})
export class WebsocketService {
public client: Client;
constructor() {
this.client = new Client({
brokerURL: 'ws://localhost:8080/ws-endpoint', // Update this URL as needed
debug: (msg: string) => {
console.log(msg);
}
});
}
connect() {
this.client.activate();
}
subscribe(destination: string, callback: (message: Message) => void) {
this.client.subscribe(destination, callback);
}
sendMessage(destination: string, message: string) {
this.client.publish({ destination, body: message });
}
}
Thank you for your help. I will provide any info needed for solving this problem.
Upvotes: 0
Views: 279
Reputation: 1
I handled the situation on this page and was successful, you can try it out Error during WebSocket handshake: Unexpected response code: 400 Spring boot websockets
Upvotes: 0