Esempio n. 1
0
  public static void main(String[] args) throws Exception {

    BufferedReader br = null;
    String line = null;

    SocketServer socketServer = new SocketServer();

    HttpServer server = HttpServer.createSimpleServer();
    server.getListener("grizzly").registerAddOn(new WebSocketAddOn());
    WebSocketEngine.getEngine().register("/", socketServer);
    server.start();

    List<WebSocket> sockets = socketServer.getSockets();
    br = new BufferedReader(new InputStreamReader(System.in));

    while ((line = br.readLine()) != null) {
      // desde el servidor al los browsers
      for (WebSocket webSocket : sockets) {
        webSocket.send(line);
      }
    }

    server.stop();
  }
 @Override
 public void write(byte[] data) throws IOException {
   if (webSocket.isConnected()) {
     webSocket.send(data);
   }
 }
 @Override
 public void write(int data) throws IOException {
   if (webSocket.isConnected()) {
     webSocket.send(String.valueOf(data));
   }
 }
 @Override
 public void onConnect(WebSocket socket) {
   LOGGER.info("Connected: {}", socket.toString());
 }
 @Override
 public void sendDone() {
   WebSocketHelper.sendData(socket, "done");
   if (socket.isConnected()) socket.close();
 }
 @Override
 public void sendError(String message) {
   if (socket.isConnected()) WebSocketHelper.sendError(socket, message);
 }