Created
November 11, 2016 07:59
-
-
Save facundofarias/7102f5120944c462a5f77a17f295c4d0 to your computer and use it in GitHub Desktop.
Enabling HK2 @Inject using WebSockets (Tyrus)
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
package cyf.rest.resources; | |
import javax.inject.Singleton; | |
import javax.websocket.server.ServerEndpointConfig.Configurator; | |
import org.glassfish.hk2.api.ServiceLocator; | |
import org.glassfish.hk2.utilities.ServiceLocatorUtilities; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
/** | |
* Instantiates WebSocket end-point with a custom injector so that @Inject can be | |
* used normally. | |
*/ | |
public class CustomConfigurator extends Configurator | |
{ | |
private ServiceLocator serviceLocator; | |
public WebsocketEndpointConfigurator() { | |
serviceLocator = ServiceLocatorUtilities.createAndPopulateServiceLocator(); | |
ServiceLocatorUtilities.enableImmediateScope(serviceLocator); | |
ServiceLocatorUtilities.bind(serviceLocator, new AbstractBinder() { | |
@Override | |
protected void configure() { | |
bind(WebSocketResource.class).to(WebSocketResource.class); | |
// Add any other bindings you might need | |
} | |
}); | |
} | |
@Override | |
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException | |
{ | |
return serviceLocator.getService(endpointClass); | |
} | |
} |
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
<!-- WebSockets libraries --> | |
<dependency> | |
<groupId>javax.websocket</groupId> | |
<artifactId>javax.websocket-api</artifactId> | |
<version>1.1</version> | |
</dependency> | |
<!-- HK2 Utilities --> | |
<dependency> | |
<groupId>org.glassfish.hk2</groupId> | |
<artifactId>hk2-utils</artifactId> | |
<version>2.5.0-b27</version> | |
</dependency> |
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
import java.io.IOException; | |
import javax.websocket.OnClose; | |
import javax.websocket.OnError; | |
import javax.websocket.OnMessage; | |
import javax.websocket.OnOpen; | |
import javax.websocket.Session; | |
import javax.websocket.server.ServerEndpoint; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
@Singleton | |
@ServerEndpoint(value = "/echo", , configurator = CustomConfigurator.class) | |
public class WebSocketResource | |
{ | |
private static final Logger log = LoggerFactory.getLogger(WebSocketResource.class); | |
// Your custom service that you want to inject | |
@Inject | |
private SomeService service; | |
/** | |
* This method is invoked when the client closes a WebSocket connection. | |
* | |
* @param session | |
* @return | |
*/ | |
@OnClose | |
public void onClose(Session session) | |
{ | |
log.info("[Session {}] Session has been closed.", session.getId()); | |
} | |
/** | |
* This method is invoked when an error was detected on the connection. | |
* | |
* @param session | |
* @param t | |
* @return | |
*/ | |
@OnError | |
public void onError(Session session, Throwable t) | |
{ | |
log.info("[Session {}] An error has been detected: {}.", session.getId(), t.getMessage()); | |
} | |
/** | |
* This method is invoked each time that the client receives a WebSocket message. | |
* | |
* @param message | |
* @param session | |
* @return | |
*/ | |
@OnMessage | |
public String onMessage(String message, Session session) | |
{ | |
log.info("[Session {}] Sending message: {}", session.getId(), message); | |
return message; // echo back the message received | |
} | |
/** | |
* OnOpen (when a socket has been opened) allows us to intercept the creation of a new session. | |
* The session class allows us to send data to the user. | |
* In the method onOpen, we'll let the user know that the handshake was | |
* successful. | |
*/ | |
@OnOpen | |
public void onOpen(Session session) | |
{ | |
log.info("[Session {}] Session has been opened.", session.getId()); | |
try { | |
session.getBasicRemote().sendText("Connection Established"); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment