Skip to content

Commit

Permalink
Very experimental WebSockets, mostly to play around with a potential API
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Oct 10, 2024
1 parent 7cdace8 commit 33d0271
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ng-adaptor-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@
<artifactId>jetty-ee10-servlet</artifactId>
<version>12.0.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10.websocket</groupId>
<artifactId>jetty-ee10-websocket-jakarta-server</artifactId>
<version>12.0.12</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
Expand All @@ -18,6 +19,8 @@

import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.Servlet;
import jakarta.websocket.server.ServerEndpointConfig;
import ng.adaptor.jetty.experimental.websockets.NGWebSocketEndpoint;
import ng.appserver.NGAdaptor;
import ng.appserver.NGApplication;
import ng.appserver.privates.NGDevelopmentInstanceStopper;
Expand Down Expand Up @@ -81,6 +84,13 @@ public void start( NGApplication application ) {

servletHandler.addServletWithMapping( servletHolder, "/" );

// FIXME: Experimental WebSocket support. Currently just for playing around // Hugi 2024-10-10
JakartaWebSocketServletContainerInitializer.configure( servletContextHandler, ( context, container ) -> {
ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder.create( NGWebSocketEndpoint.class, "/ng-websocket" ).build();
container.addEndpoint( endpointConfig );
} );
// FIXME: Experimental WebSocket support // Hugi 2024-10-10

try {
server.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ng.adaptor.jetty.experimental.websockets;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.websocket.CloseReason;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.RemoteEndpoint;
import jakarta.websocket.Session;

public class NGWebSocketEndpoint extends Endpoint implements MessageHandler.Whole<String> {

public static List<NGWebSocketEndpoint> allEndpoints = new ArrayList<>();

private static final Logger logger = LoggerFactory.getLogger( NGWebSocketEndpoint.class );
public Session _session;
public RemoteEndpoint.Async _remoteEndpoint;

@Override
public void onOpen( Session session, EndpointConfig config ) {
_session = session;
_remoteEndpoint = session.getAsyncRemote();
logger.info( "Opened websocket: {}", session );
session.addMessageHandler( this );
_remoteEndpoint.sendText( "You are connected to " + this.getClass().getName() );

allEndpoints.add( this );
}

@Override
public void onClose( Session session, CloseReason close ) {
super.onClose( session, close );
_session = null;
_remoteEndpoint = null;
logger.info( "Closing websocket {} - {}", close.getCloseCode(), close.getReasonPhrase() );
}

@Override
public void onMessage( String message ) {
_remoteEndpoint.sendText( message );
logger.info( "Client sent message: '{}'", message );
}

@Override
public void onError( Session session, Throwable throwable ) {
super.onError( session, throwable );
logger.error( "websocket error", throwable );
}
}

0 comments on commit 33d0271

Please sign in to comment.