本文整理匯總了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();
}
示例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);
}
}