Trying to make a minimal working example with web sockets. On the server side, I use the Spring Framework , the client uses JavaScript .
- Server
To the simple Spring Boot project I added a class with the configuration:
@Configuration public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(notificationWebSocketHandler(), "/notification").setAllowedOrigins("*").withSockJS(); } @Bean public NotificationWebSocketHandler notificationWebSocketHandler() { return new NotificationWebSocketHandler(); } } public class NotificationWebSocketHandler extends TextWebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { System.out.println("Connection established"); for (int i = 0 ; i < 5; i++) { TextMessage message = new TextMessage("Notification " + i); session.sendMessage(message); try { Thread.sleep(3000); } catch (Exception ex) { System.out.println("An error occurred"); session.close(CloseStatus.SERVER_ERROR); } } session.close(CloseStatus.NORMAL); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { System.out.println("Connection closed with the status: " + status); } } Client
index.html<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script> <!-- <script src="stomp.min.js"></script> --> <script> var url = 'http://localhost:8080/notification'; var socket = new SockJS(url); socket.onopen = function() { console.log('Opening...'); }; socket.onclose = function() { console.log('Closing...'); }; socket.onmessage = function (event) { console.log('Received notification: ', event.data); } </script> </head> <body></body>
When opening the index.html file in the browser console, errors are displayed:
GET http: // localhost: 8080 / notification / info? T = 1554898230939 404
Access to XMLHttpRequest at ' http: // localhost: 8080 / notification / info? T = 1554898230939 ' from origin ' http: // localhost: 8081 ' has been blocked by the CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

.setAllowedOrigins("*")allows you to make a request from any domain. Or am I misunderstanding something? - not a ProgrammerXMLHttpRequestdoes not allow making a request to another domain. port? - not a Programmer@Configuration class WebConfig : WebMvcConfigurer { override fun addCorsMappings(registry: CorsRegistry) { registry .addMapping("/**") .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS") .allowedOrigins( "http://localhost:4200", "http://localhost:80" ) .allowedHeaders("*") .allowCredentials(true) } }- YuriySPb ♦CORSgone. But now I get 404 at the address " localhost: 8080 / notification / info? T = 1554961148178 ". I can not understand why "/ info? T = 1554961148178" is added, because I am trying to connect a socket to the address " localhost: 8080 / notification "? - not a Programmer