示例#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));
   }
 }