最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - angular10 springboot websocket connection - Stack Overflow

programmeradmin2浏览0评论

i want to configure a simple websocket for notification.. in springboot i created two files webSocketConfig and webSocketController

package com.example.demo.websocket;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new WebSocketController(), "/ws")
                .setAllowedOrigins("*");// i t
    }
}


package com.example.demo.websocket;


import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class WebSocketController extends TextWebSocketHandler {

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {
        // Echo back the message to the client
        try {
            session.sendMessage(new TextMessage("Echo: " + message.getPayload()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

port : Tomcat started on port(s): 8088 (http) with context path ''

when i tried to test it in hopposcotch i write ws://localhost:8088/ws and i got error in connection(something was wrong and then disconnected)..any help plz

发布评论

评论列表(0)

  1. 暂无评论