コード例 #1
0
  /**
   * The main method.
   *
   * @param argv the arguments
   * @throws Exception the exception
   */
  public static void main(String[] argv) throws Exception {
    System.setProperty("websocket.packatdump", String.valueOf(PacketDumpUtil.ALL));
    //		System.setProperty("javax.net.debug", "all");
    System.setProperty("java.util.logging.config.file", "logging.properties");
    WebSocket socket =
        WebSockets.createDraft06(
            "wss://localhost:8443/ws/",
            new WebSocketHandler() {

              public void onOpen(WebSocket socket) {
                System.err.println("Open");
                try {
                  socket.send(socket.createFrame(System.getenv("USER") + ":has joined!"));
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }

              public void onMessage(WebSocket socket, Frame frame) {
                if (!frame.toString().startsWith(System.getenv("USER"))) {
                  try {
                    socket.send(
                        socket.createFrame(System.getenv("USER") + ":(echo)" + frame.toString()));
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                }
                System.out.println(frame);
              }

              public void onError(WebSocket socket, WebSocketException e) {
                e.printStackTrace();
              }

              public void onClose(WebSocket socket) {
                System.err.println("Closed");
              }

              public void onCloseFrame(WebSocket socket, int statusCode, String reason) {
                // TODO Auto-generated method stub

              }
            },
            "chat");
    socket.setBlockingMode(true);
    socket.connect();
  }
コード例 #2
0
  /**
   * The main method.
   *
   * @param argv the arguments
   * @throws Exception the exception
   */
  public static void main(String[] argv) throws Exception {
    System.setProperty("websocket.packatdump", String.valueOf(PacketDumpUtil.ALL));
    //		System.setProperty("javax.net.debug", "all");
    System.setProperty("java.util.logging.config.file", "logging.properties");
    //		Proxy proxy = new Proxy(new Credentials("test", "test"), new BasicAuthenticator());
    // SpengoAuthenticator.setDefaultMechanism(Mechanism.NTLM);
    Proxy proxy =
        new Proxy(
            new InetSocketAddress("192.168.100.230", 8080),
            new Credentials("test", "test"),
            new DefaultAuthenticator());
    //		Proxy proxy = new Proxy(new Credentials("test", "test"), new DigestAuthenticator());
    WebSocket socket =
        WebSockets.create(
            "wss://echo.websocket.org",
            "http://www.websocket.org",
            proxy,
            new WebSocketHandler() {

              public void onOpen(WebSocket socket) {
                System.err.println("Open");
                try {
                  socket.send(socket.createFrame("Test"));
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }

              public void onMessage(WebSocket socket, Frame frame) {
                System.out.println(frame);
                try {
                  Thread.sleep(5000);
                } catch (InterruptedException e) {
                  e
                      .printStackTrace(); // To change contents of catch statement use File |
                                          // Settings | File Templates.
                }
                try {
                  socket.send(socket.createFrame("Boo"));
                } catch (WebSocketException e) {
                  e
                      .printStackTrace(); // To change contents of catch statement use File |
                                          // Settings | File Templates.
                }
              }

              public void onError(WebSocket socket, WebSocketException e) {
                e.printStackTrace();
              }

              public void onClose(WebSocket socket) {
                System.err.println("Closed");
              }

              public void onCloseFrame(WebSocket socket, int statusCode, String reason) {
                // TODO Auto-generated method stub

              }
            });
    socket.setBlockingMode(true);
    socket.connect();
  }