当前位置: 首页>>代码示例>>Java>>正文


Java WebSocketServlet类代码示例

本文整理汇总了Java中org.eclipse.jetty.websocket.servlet.WebSocketServlet的典型用法代码示例。如果您正苦于以下问题:Java WebSocketServlet类的具体用法?Java WebSocketServlet怎么用?Java WebSocketServlet使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


WebSocketServlet类属于org.eclipse.jetty.websocket.servlet包,在下文中一共展示了WebSocketServlet类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: startHttpServer

import org.eclipse.jetty.websocket.servlet.WebSocketServlet; //导入依赖的package包/类
@Override
public void startHttpServer() {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            Server server = new Server(23333);
            try {
                ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
                context.setContextPath("/");
                ServletHolder wsHolder = new ServletHolder("echo", new WebSocketServlet() {
                    @Override
                    public void configure(WebSocketServletFactory factory) {
                        factory.register(WebSocketHandler.class);
                    }
                });
                context.addServlet(wsHolder, "/w");

                URI uri = OmniOcular.class.getResource("/assets/omniocular/static/").toURI();
                context.setBaseResource(Resource.newResource(uri));
                context.setWelcomeFiles(new String[]{"index.html"});
                ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
                holderPwd.setInitParameter("cacheControl", "max-age=0,public");
                holderPwd.setInitParameter("useFileMappedBuffer", "false");
                context.addServlet(holderPwd, "/");

                server.setHandler(context);
                server.start();
                server.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    t.start();

}
 
开发者ID:exzhawk,项目名称:OmniOcular,代码行数:37,代码来源:ClientProxy.java

示例2: start

import org.eclipse.jetty.websocket.servlet.WebSocketServlet; //导入依赖的package包/类
@Override
public void start() {
    if (server != null && server.isRunning()) {
        return;
    }
    server = new Server();
    ServerConnector connector;

    if (address.getProtocol().equals(WebSocketProtocol.WSS)) {
        SslContextFactory sslContextFactory = new SslContextFactory();
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory,
                                                                             HttpVersion.HTTP_1_1.asString());
        connector = new ServerConnector(server, sslConnectionFactory);
    } else {
        connector = new ServerConnector(server);
    }
    connector.setPort(address.getPort());
    server.addConnector(connector);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    ServletHolder holderEvents = new ServletHolder("ws-events", new WebSocketServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        public void configure(WebSocketServletFactory webSocketServletFactory) {
            webSocketServletFactory.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
            webSocketServletFactory.getPolicy().setMaxTextMessageSize(maxMessageSize);
            webSocketServletFactory.setCreator(new WebSocketCreator() {

                @Override
                public Object createWebSocket(ServletUpgradeRequest servletUpgradeRequest,
                                              ServletUpgradeResponse servletUpgradeResponse) {
                    CCWebSocketMessagingSkeletonSocket socket = new CCWebSocketMessagingSkeletonSocket(WebSocketJettyServer.this);
                    openSockets.add(socket);
                    return socket;
                }
            });
        }
    });

    context.addServlet(holderEvents, address.getPath());

    try {
        server.start();
    } catch (Exception t) {
        logger.error("Error while starting websocket server: ", t);
    }
}
 
开发者ID:bmwcarit,项目名称:joynr,代码行数:53,代码来源:WebSocketJettyServer.java


注:本文中的org.eclipse.jetty.websocket.servlet.WebSocketServlet类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。