-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very experimental WebSockets, mostly to play around with a potential API
- Loading branch information
1 parent
7cdace8
commit 33d0271
Showing
3 changed files
with
69 additions
and
0 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
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
54 changes: 54 additions & 0 deletions
54
...tor-jetty/src/main/java/ng/adaptor/jetty/experimental/websockets/NGWebSocketEndpoint.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,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 ); | ||
} | ||
} |