WebSocket客户端java代码

Maven引入websoket的jar包:

 
  1. <dependency>

  2. <groupId>org.java-websocket</groupId>

  3. <artifactId>Java-WebSocket</artifactId>

  4. </dependency>


 

客户端代码:

 
  1. package com.cms.integration.common;

  2.  
  3. import java.net.URI;

  4. import java.net.URISyntaxException;

  5.  
  6. import org.java_websocket.client.WebSocketClient;

  7. import org.java_websocket.drafts.Draft;

  8. import org.java_websocket.drafts.Draft_17;

  9. import org.java_websocket.handshake.ServerHandshake;

  10.  
  11. public class WebSocketC extends WebSocketClient{

  12.  
  13. /**

  14. * 接受websocket服务端的值.

  15. */

  16. private static String receiveData = null;

  17.  
  18. /**

  19. * 判断WebSocket是否打开.

  20. */

  21. private static boolean isOpen = false;

  22.  
  23. public WebSocketC(URI serverURI){

  24. super(serverURI,new Draft_17());

  25. }

  26.  
  27. public WebSocketC(URI serverURI, Draft draft) {

  28. super( serverURI, draft );

  29. }

  30.  
  31. @Override

  32. public void onClose( int code, String reason, boolean remote ) {

  33. // The codecodes are documented in class org.java_websocket.framing.CloseFrame

  34. System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) );

  35. isOpen = false;

  36. }

  37.  
  38. @Override

  39. public void onError(Exception arg0) {

  40. // TODO Auto-generated method stub

  41. System.out.println("make mistakes");

  42. }

  43.  
  44. @Override

  45. public void onMessage(String message) {

  46. // TODO Auto-generated method stub

  47. System.out.println( "received: " + message );

  48. isOpen = true;

  49. setReceiveData(message);

  50. }

  51.  
  52. @Override

  53. public void onOpen(ServerHandshake arg0) {

  54. // TODO Auto-generated method stub

  55. System.out.println( "opened connection" );

  56. }

  57.  
  58. public static String getReceiveData() {

  59. return receiveData;

  60. }

  61.  
  62. public void setReceiveData(String receiveData) {

  63. WebSocketC.receiveData = receiveData;

  64. }

  65.  
  66. public static boolean getWebSocketConnectionIsOpen(){

  67. return isOpen;

  68. }

  69.  
  70. public static void main( String[] args ) throws URISyntaxException, InterruptedException {

  71. // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts

  72. WebSocketC websocket = new WebSocketC( new URI( "ws://192.168.14.119:8090/loginVerify" ));

  73. if(!websocket.connectBlocking()){

  74. System.err.println( "Could not connect to the server." );

  75. return;

  76. }

  77.  
  78. // while(true){

  79. // String value = getReceiveData();

  80. // if(value == null){

  81. // value =getReceiveData();

  82. // }else{

  83. // System.out.println(value+"__________________");

  84. // return;

  85. // }

  86. // }

  87.  
  88. // 2秒后关闭

  89. Thread.sleep(2000);

  90. websocket.closeBlocking();

  91.  
  92. boolean f = getWebSocketConnectionIsOpen();

  93. if(!f){

  94. WebSocketC websockets = new WebSocketC( new URI( "ws://192.168.14.119:8090/loginVerify" ));

  95. websockets.connectBlocking();

  96. }

  97.  
  98. }

  99. }

猜你喜欢

转载自blog.csdn.net/Amelia__Liu/article/details/82151798
今日推荐