當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。