客户端:
package websocketclient; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; /** * @Author * @Description * @Date 2018/12/28 0028上午 10:03 **/ public class MyWebSocketClient extends WebSocketClient { public MyWebSocketClient(String url) throws URISyntaxException { super(new URI(url)); // 连接地址 } @Override public void onOpen(ServerHandshake shake) { System.out.println("客户端连接了服务器"); } @Override public void onMessage(String paramString) { System.out.println("接收到消息:"+paramString); } @Override public void onClose(int paramInt, String paramString, boolean paramBoolean) { System.out.println("服务器关闭..."); } @Override public void onError(Exception e) { System.out.println("连接异常"+e); } @Override public void onMessage(ByteBuffer bytes) { super.onMessage(bytes); System.err.println("这里是onmessage:"); } }
模拟用户
package websocketclient; import org.java_websocket.WebSocket; import java.net.URISyntaxException; /** * @Author * @Description * @Date 2018/12/28 0028下午 1:30 * 模拟用户操作 **/ public class main { public static void main(String[] args) throws URISyntaxException { MyWebSocketClient client = new MyWebSo