【问题标题】:WebSockets JSR 356 Spring integration @ServerEndpointWebSockets JSR 356 Spring 集成 @ServerEndpoint
【发布时间】:2016-03-18 17:09:16
【问题描述】:

问题:@ServerEndpoint 类中的 @Autowired bean 为空

我怎样才能确保下面的这个 WebSocketController 类将被注入 bean,那我怎样才能让它由 Spring 管理?我可以连接到 websocket,所以它可以工作,但是在 WebSocketController 类实例中,gameService 始终为空,所以我认为它是由 tomcat 以某种方式创建的,而不是 Spring。

我正在使用 Spring Boot。我只需要弄清楚如何将 bean 注入这个 websocket 控制器类。

WebSocketController 类

@Component
@ServerEndpoint("/sock")
public class WebSocketController {

    @Autowired
    private GameService gameService;

    private static Set<Session> clients = Collections.synchronizedSet(new HashSet<Session>());

    @OnMessage
    public void handleMessage(Session session, String message) throws IOException {

        session.getBasicRemote().sendText(
                "Reversed: " + new StringBuilder(message).reverse());
    }

    @OnOpen
    public void onOpen(Session session) {
        clients.add(session);
        System.out.println("New client @"+session.getId());
        if (gameService == null) System.out.println("game service null");
    }

    @OnClose
    public void onClose(Session session) {
        clients.remove(session);
        System.out.println("Client disconnected @" + session.getId());
    }
}

GameService接口及实现

public interface GameService {
    List<Character> getCharacters();
}

@Service
public class GameServiceMockImpl implements GameService {

    @Override
    public List<Character> getCharacters() {
        List<Character> list = new ArrayList<>();
        list.add(new Character("aaa","1.png",100));
        list.add(new Character("aaa","2.jpg",100));
        list.add(new Character("aaa","3.jpg",100));
        return list;
    }
}

应用类

@SpringBootApplication
public class App  {

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

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

编辑:

使用 Spring 4 WebSockets 根本不起作用,我什至无法通过浏览器连接。

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myHandler(), "/myHandler");
    }

    @Bean
    public WebSocketHandler myHandler() {
        return new MyHandler();
    }

}


public class MyHandler extends TextWebSocketHandler {

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {
        System.out.println(message.getPayload());
    }

}

【问题讨论】:

    标签: spring spring-mvc tomcat websocket jsr


    【解决方案1】:

    您正在尝试集成 Spring 和 Java WebSocket API。由@Component 注释的类注册到spring bean,它的实例由spring 管理,但如果一个类由@ServerEndpoint 注释,它会注册到服务器端WebSocket 端点并且每次对应端点的WebSocket 连接到服务器,其实例由 JWA 实现创建和管理。我们不能同时使用这两个注释。

    你可以使用CDI注入(你的服务器也应该支持)

    @ServerEndpoint("/sock")
    public class WebSocketController {
    
        @Inject
        private GameService gameService;
    

    或者看看this doc,Spring 4 已经支持 WebSocket

    【讨论】:

    • 感谢您的回答。我宁愿使用 Spring 4 WebSocket,我想使用没有 SockJS 之类的“直接方法”。所以我创建了 TextWebSocketHandler 和 WebSocketConfigurer。但它不起作用。我无法从浏览器连接。它在控制台中说它将给定的 url 映射到文本处理程序,但仍然无法连接。我在主帖中添加了代码。
    • 我必须允许来自其他来源的连接并且它有效。所以最后我使用的是 Spring 4 websockets。
    • 但仅用于测试。最终一切都会在服务器上
    【解决方案2】:

    也许这篇文章可以帮助:

    https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support

    你可以使用依赖(spring版本> 4)

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
        <version>${spring.version}</version>
    </dependency>
    

    然后简单地

    @ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class)
    public class WebSocketEndpoint {
    
        @Inject
        private BroadcastService broadcaster;
    

    【讨论】:

    • 简单又好用。那就是让它对我有用的那个!
    • 你能简单解释一下这段代码吗?拜托!我不知道如何使用这段代码。
    • @Sushant 你可以阅读文章spring.io/blog/2013/05/23/… - 那里的所有解释都是最好的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多