-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
2,253 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
embedded/websocket-jakarta-api/src/main/java/examples/annotated/EchoSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package examples.annotated; | ||
|
||
import jakarta.websocket.CloseReason; | ||
import jakarta.websocket.OnClose; | ||
import jakarta.websocket.OnError; | ||
import jakarta.websocket.OnMessage; | ||
import jakarta.websocket.OnOpen; | ||
import jakarta.websocket.RemoteEndpoint; | ||
import jakarta.websocket.Session; | ||
import jakarta.websocket.server.ServerEndpoint; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@ServerEndpoint("/echo") | ||
public class EchoSocket | ||
{ | ||
private static final Logger LOG = LoggerFactory.getLogger(EchoSocket.class); | ||
private Session session; | ||
private RemoteEndpoint.Async remote; | ||
|
||
@OnClose | ||
public void onWebSocketClose(CloseReason close) | ||
{ | ||
this.session = null; | ||
this.remote = null; | ||
LOG.info("WebSocket Close: {} - {}", close.getCloseCode(), close.getReasonPhrase()); | ||
} | ||
|
||
@OnOpen | ||
public void onWebSocketOpen(Session session) | ||
{ | ||
this.session = session; | ||
this.remote = this.session.getAsyncRemote(); | ||
LOG.info("WebSocket Connect: {}", session); | ||
this.remote.sendText("You are now connected to " + this.getClass().getName()); | ||
} | ||
|
||
@OnError | ||
public void onWebSocketError(Throwable cause) | ||
{ | ||
LOG.warn("WebSocket Error", cause); | ||
} | ||
|
||
@OnMessage | ||
public String onWebSocketText(String message) | ||
{ | ||
LOG.info("Echoing back text message [{}]", message); | ||
// Using shortcut approach to sending messages. | ||
// You could use a void method and use remote.sendText() | ||
return message; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
embedded/websocket-jakarta-api/src/main/java/examples/annotated/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package examples.annotated; | ||
|
||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.servlet.DefaultServlet; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; | ||
|
||
public class Main | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
Server server = new Server(8080); | ||
server.start(); | ||
server.join(); | ||
} | ||
|
||
public static Server newServer(int port) | ||
{ | ||
Server server = new Server(port); | ||
|
||
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); | ||
servletContextHandler.setContextPath("/"); | ||
server.setHandler(servletContextHandler); | ||
|
||
// Add jakarta.websocket support | ||
JakartaWebSocketServletContainerInitializer.configure(servletContextHandler, (context, container) -> | ||
{ | ||
// Add echo endpoint to server container | ||
container.addEndpoint(EchoSocket.class); | ||
}); | ||
|
||
// Add default servlet (to serve the html/css/js) | ||
// Figure out where the static files are stored. | ||
URL urlStatics = Thread.currentThread().getContextClassLoader().getResource("echo-root/index.html"); | ||
Objects.requireNonNull(urlStatics, "Unable to find index.html in classpath"); | ||
String urlBase = urlStatics.toExternalForm().replaceFirst("/[^/]*$", "/"); | ||
ServletHolder defHolder = new ServletHolder("default", new DefaultServlet()); | ||
defHolder.setInitParameter("resourceBase", urlBase); | ||
defHolder.setInitParameter("dirAllowed", "true"); | ||
servletContextHandler.addServlet(defHolder, "/"); | ||
|
||
return server; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
embedded/websocket-jakarta-api/src/main/java/examples/endpoint/EchoSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package examples.endpoint; | ||
|
||
import jakarta.websocket.CloseReason; | ||
import jakarta.websocket.Endpoint; | ||
import jakarta.websocket.EndpointConfig; | ||
import jakarta.websocket.MessageHandler; | ||
import jakarta.websocket.RemoteEndpoint; | ||
import jakarta.websocket.Session; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class EchoSocket extends Endpoint implements MessageHandler.Whole<String> | ||
{ | ||
private static final Logger LOG = LoggerFactory.getLogger(EchoSocket.class); | ||
private Session session; | ||
private RemoteEndpoint.Async remote; | ||
|
||
@Override | ||
public void onClose(Session session, CloseReason close) | ||
{ | ||
super.onClose(session, close); | ||
this.session = null; | ||
this.remote = null; | ||
LOG.info("WebSocket Close: {} - {}", close.getCloseCode(), close.getReasonPhrase()); | ||
} | ||
|
||
@Override | ||
public void onOpen(Session session, EndpointConfig config) | ||
{ | ||
this.session = session; | ||
this.remote = this.session.getAsyncRemote(); | ||
LOG.info("WebSocket Open: {}", session); | ||
// attach echo message handler | ||
session.addMessageHandler(this); | ||
this.remote.sendText("You are now connected to " + this.getClass().getName()); | ||
} | ||
|
||
@Override | ||
public void onError(Session session, Throwable cause) | ||
{ | ||
super.onError(session, cause); | ||
LOG.warn("WebSocket Error", cause); | ||
} | ||
|
||
@Override | ||
public void onMessage(String message) | ||
{ | ||
LOG.info("Echoing back text message [{}]", message); | ||
this.remote.sendText(message); | ||
} | ||
} |
Oops, something went wrong.