首页 > 解决方案 > Spring Boot WebSocket 无响应

问题描述

我有一个处理文件上传和数据存储的 Spring Boot 应用程序。我已包含 WebSocket 配置,但 WebSocket 无法正常工作。我的 Android 应用程序抛出异常,提示“流路径为空。

处理程序类

import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class SocketHandler extends TextWebSocketHandler {

List sessions = new ArrayList<>();

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message)
        throws InterruptedException, IOException {

    for(Object t : sessions) {
                WebSocketSession webSocketSession = (WebSocketSession) t;
        Map value = new Gson().fromJson(message.getPayload(), Map.class);
        webSocketSession.sendMessage(new TextMessage("Hello " + value.get("name") + " !"));
    }
}

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    //the messages will be broadcasted to all users.
    sessions.add(session);
}
}

配置类

import com.labafrique.creporter.web.CReporterSpring.websocket.SocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
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
@Controller
public class WebSocketConfig implements WebSocketConfigurer {
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new SocketHandler(), "/t").setAllowedOrigins("*");
    }
}

主班

import com.labafrique.creporter.web.CReporterSpring.controller.property.FileStorageProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories("com.labafrique.creporter.web.CReporterSpring.repository") 
@EntityScan("com.labafrique.creporter.web.CReporterSpring.model")
@ComponentScan(basePackages = {"com.labafrique.creporter.web.CReporterSpring.controller"})
@EnableConfigurationProperties({
        FileStorageProperties.class
}) 
@SpringBootApplication
public class CReporterSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(CReporterSpringApplication.class, args);
    }
}

我试过在不同的包中移动类,但仍然没有任何效果。

标签: javaspringspring-bootwebsocketspring-websocket

解决方案


推荐阅读