用Java分别实现WebSocket客户端与服务端(2)

import java.net.URI;
import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.log4j.Logger;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

public class MyWebSocketClient extends WebSocketClient{

  Logger logger = Logger.getLogger(MyWebSocketClient.class);
 
  public MyWebSocketClient(URI serverUri) {
     super(serverUri);
  }

  @Override
  public void onOpen(ServerHandshake arg0) {
    // TODO Auto-generated method stub
    logger.info("------ MyWebSocket onOpen ------");
  }

  @Override
  public void onClose(int arg0, String arg1, boolean arg2) {
     // TODO Auto-generated method stub
     logger.info("------ MyWebSocket onClose ------");
  }

  @Override
  public void onError(Exception arg0) {
     // TODO Auto-generated method stub
     logger.info("------ MyWebSocket onError ------");
  }

  @Override
  public void onMessage(String arg0) {
     // TODO Auto-generated method stub
     logger.info("-------- 接收到服务端数据: " + arg0 + "--------");
  }
}

MyTest 测试类

public class MyTest{

  public static void main(String[] arg0){
    MyWebSocketClient myClient = new MyWebSocketClient("此处为websocket服务端URI");
    // 往websocket服务端发送数据
    myClient.send("此为要发送的数据内容");
  }

}

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f2717ed1f4cc7c3ba6e49440ebf1b3f4.html