package com.ev3.brick; import java.io.IOException; import javax.ejb.Singleton; import javax.inject.Inject; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @Singleton @ServerEndpoint("/angular") public class AngularConnection { @Inject BrickConnection BC; private Session session; /** * @OnOpen 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) { this.session = session; System.out.println(session.getId() + " has opened an angular connection"); try { session.getBasicRemote().sendText("Connection Established"); } catch (IOException ex) { ex.printStackTrace(); } } public void sendCommand(String command) throws IOException { try { session.getBasicRemote().sendText(command); } catch (IOException ex) { ex.printStackTrace(); } } @OnMessage public void onMessage(String message, Session session) { System.out.println("Message from " + session.getId() + ": " + message); } @OnClose public void onClose(Session session) { System.out.println("Session " + session.getId() + " has ended"); } }