将JSR-356 WebSocket @ServerEndpoint与Spring 3 bean集成 [英] Integrating JSR-356 WebSocket @ServerEndpoint with Spring 3 beans

查看:257
本文介绍了将JSR-356 WebSocket @ServerEndpoint与Spring 3 bean集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring 3.2.5而没有全新的JSR-356 WebSockets支持。

I'm using Spring 3.2.5 without full new JSR-356 WebSockets support.

我想在我的<$ c $中使用singleton-bean引用c> @ServerEndpoint WebSocket服务器,由servlet容器本身实例化,不在Spring上下文中。

I would like to have singleton-bean reference in my @ServerEndpoint WebSocket server, which is instantiated by servlet container itself, not in Spring context.

什么是干净的方式要做到这一点?

我当前的解决方案:我在静态实例中创建了 @Service 单例bean字段:

My current solution: I made @Service singleton bean with instance in static field:

@Service
public class WebSocketSupportBean {
    private volatile static WebSocketSupportBean instance = null;

    public static WebSocketSupportBean getInstance() {
        return instance;
    }

    public WebSocketSupportBean() {
        instance = this;
    }

只需在 @ServerEndpoint 通过静态方法,如果返回null则断开用户连接(如果在服务器启动期间bean没有创建但用户连接时):

and just getting it in @ServerEndpoint by static method, disconnecting user if null returned (if bean not jet created during server startup but user connects):

推荐答案

您可以使用spring framework 3.x设置websockets。

You can setup websockets with spring framework 3.x

我开发了一个小概念验证应用程序,用于演示如何基于Rossen Stoyanchev的SpringConfiguration发布 - 核心4.0。

I developed a small proof-of-concept application to demonstrate how, based on Rossen Stoyanchev's SpringConfiguration released with spring-core 4.0.

应用程序使用uri / wstest 设置websocket服务器端点,该端点将使用 @Autowired spring bean选择问候语并回复websocket消息。

The application sets up a websocket server endpoint with uri /wstest which will use a @Autowired spring bean to select a greeting word and reply to a websocket message.

启动websocket连接并发送消息在支持websockets的浏览器中运行的html页面( index.html )。

The websocket connection is initiated and messages sent by an html page (index.html) running in a browser that supports websockets.

端点注册由上下文初始化时以及端点为ins时的ServletContextListener它将与spring连接:

The Endpoint registration is made by a ServletContextListener at context initialization and when the endpoint is instantiated it will be wired with spring:

@WebListener
public class MyApplication implements ServletContextListener {

    private final static String SERVER_CONTAINER_ATTRIBUTE = "javax.websocket.server.ServerContainer";

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        ServletContext container = sce.getServletContext();

        final ServerContainer serverContainer = (ServerContainer) container.getAttribute(SERVER_CONTAINER_ATTRIBUTE);
        try {
            serverContainer.addEndpoint(new MyEndpointConfig(MyEndpoint.class, "/wstest"));
        } catch (DeploymentException e) {
            e.printStackTrace();
        }
    }
}

端点是:

@Component
public class MyEndpoint extends Endpoint {

    @Autowired
    MyService myService;

    @Override
    public void onOpen(Session session, EndpointConfig config) {

        session.addMessageHandler(new MyMessageHandler(session));
    }


    class MyMessageHandler implements MessageHandler.Whole<String> {

        final Session session;

        public MyMessageHandler(Session session) {
            this.session = session;
        }

        @Override
        public void onMessage(String message) {
            try {
                String greeting = myService.getGreeting();
                session.getBasicRemote().sendText(greeting + ", got your message (" + message + "). Thanks ! (session: " + session.getId() + ")");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

结帐我的 Github页面上的完整资源并准备好运行示例。

Checkout the full source and ready to run example on my Github page.

这篇关于将JSR-356 WebSocket @ServerEndpoint与Spring 3 bean集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆