• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

WebSocket工具类

武飞扬头像
jsxin0816
帮助1

        最近的项目在整长连接WebSocket,之前也写过一个感觉没有这个全面。提供个工具类WebSocketHelperJava-WebSocket-1.3.9.jar包以及一个HttpURLConnectionUtil

1、WebSocketHelper

  1.  
    import android.util.Log;
  2.  
     
  3.  
    import org.java_websocket.client.WebSocketClient;
  4.  
    import org.java_websocket.drafts.Draft;
  5.  
    import org.java_websocket.drafts.Draft_6455;
  6.  
    import org.java_websocket.handshake.ServerHandshake;
  7.  
     
  8.  
    import java.net.URI;
  9.  
    import java.util.Date;
  10.  
    import java.util.concurrent.TimeUnit;
  11.  
     
  12.  
    /**
  13.  
    * author : jsxin
  14.  
    * e-mail : jsxin0816@163.com
  15.  
    * time : 2023/07/03
  16.  
    * desc : WebSocketHelper工具类
  17.  
    */
  18.  
    public class WebSocketHelper {
  19.  
    public static final String TAG = WebSocketHelper.class.getSimpleName();
  20.  
    private RobotWebSocket ws_robot;
  21.  
    private boolean isRunning;
  22.  
    private boolean isInterrupt;
  23.  
    public static boolean isOpenOk = false;
  24.  
     
  25.  
    private WebSocketHelper() {
  26.  
    this.isRunning = false;
  27.  
    this.isInterrupt = false;
  28.  
    }
  29.  
     
  30.  
    public static WebSocketHelper getInstance() {
  31.  
    return SingletonHolder.instance;
  32.  
    }
  33.  
     
  34.  
    public boolean isRunning() {
  35.  
    return this.isRunning;
  36.  
    }
  37.  
     
  38.  
    public void ConnectService(final String serive_ip, final String pad_name, final WebSokcetCallback cb) {
  39.  
    if (!this.isRunning) {
  40.  
    this.isInterrupt = false;
  41.  
    Runnable runnable = new Runnable() {
  42.  
    public void run() {
  43.  
    while(!WebSocketHelper.this.isInterrupt) {
  44.  
    WebSocketHelper.this.isRunning = true;
  45.  
    if (WebSocketHelper.isOpenOk) {
  46.  
    try {
  47.  
    Thread.sleep(2000L);
  48.  
    } catch (Exception var2) {
  49.  
    var2.printStackTrace();
  50.  
    }
  51.  
    } else {
  52.  
    try {
  53.  
    String url = "ws://" serive_ip "/webSocket/" 1;
  54.  
    System.out.print(url);
  55.  
    Log.i(WebSocketHelper.TAG, url);
  56.  
    WebSocketHelper.this.ws_robot = WebSocketHelper.this.new RobotWebSocket(new URI(url), new Draft_6455(), cb);
  57.  
    WebSocketHelper.this.ws_robot.connectBlocking(10L, TimeUnit.SECONDS);
  58.  
    } catch (Exception var4) {
  59.  
    Log.e(WebSocketHelper.TAG, var4.getMessage());
  60.  
    }
  61.  
     
  62.  
    try {
  63.  
    Thread.sleep(2000L);
  64.  
    } catch (Exception var3) {
  65.  
    var3.printStackTrace();
  66.  
    }
  67.  
    }
  68.  
    }
  69.  
     
  70.  
    WebSocketHelper.this.isRunning = false;
  71.  
    Log.i(WebSocketHelper.TAG, "robot websocket run over");
  72.  
    }
  73.  
    };
  74.  
    Thread thread = new Thread(runnable);
  75.  
    thread.start();
  76.  
    }
  77.  
    }
  78.  
     
  79.  
    public int sendMsg(String message) {
  80.  
    Log.d(TAG, "message==" message);
  81.  
    synchronized(this.ws_robot) {
  82.  
    try {
  83.  
    if (this.ws_robot != null) {
  84.  
    this.ws_robot.send(message);
  85.  
    }
  86.  
    } catch (Exception var5) {
  87.  
    var5.printStackTrace();
  88.  
    isOpenOk = false;
  89.  
    return 1;
  90.  
    }
  91.  
     
  92.  
    return 0;
  93.  
    }
  94.  
    }
  95.  
     
  96.  
    public void destroy() {
  97.  
    try {
  98.  
    this.isInterrupt = true;
  99.  
    if (this.ws_robot != null) {
  100.  
    this.ws_robot.closeBlocking();
  101.  
    }
  102.  
    } catch (Exception var2) {
  103.  
    var2.printStackTrace();
  104.  
    }
  105.  
     
  106.  
    }
  107.  
     
  108.  
    private class RobotWebSocket extends WebSocketClient {
  109.  
    private WebSokcetCallback cb_;
  110.  
     
  111.  
    RobotWebSocket(URI serverUri, Draft protocolDraft, WebSokcetCallback cb) {
  112.  
    super(serverUri);
  113.  
    this.cb_ = cb;
  114.  
    }
  115.  
     
  116.  
    public void onOpen(ServerHandshake handshakedata) {
  117.  
    Log.i(WebSocketHelper.TAG, "onOpen");
  118.  
    WebSocketHelper.isOpenOk = true;
  119.  
    if (this.cb_ != null) {
  120.  
    this.cb_.onConnect();
  121.  
    }
  122.  
     
  123.  
    }
  124.  
     
  125.  
    public void onMessage(String message) {
  126.  
    Log.d(WebSocketHelper.TAG, "receive: " message);
  127.  
    if (this.cb_ != null) {
  128.  
    this.cb_.onMessageData(message);
  129.  
    }
  130.  
     
  131.  
    }
  132.  
     
  133.  
    public void onClose(int code, String reason, boolean remote) {
  134.  
    Log.i(WebSocketHelper.TAG, "Connection close by " (remote ? "remote peer" : "us") " at " new Date(System.currentTimeMillis()));
  135.  
    WebSocketHelper.isOpenOk = false;
  136.  
    if (this.cb_ != null) {
  137.  
    this.cb_.onDisonnect();
  138.  
    }
  139.  
     
  140.  
    }
  141.  
     
  142.  
    public void onError(Exception ex) {
  143.  
    Log.e(WebSocketHelper.TAG, "onError" ex.getMessage());
  144.  
    WebSocketHelper.isOpenOk = false;
  145.  
    if (this.cb_ != null) {
  146.  
    this.cb_.onDisonnect();
  147.  
    }
  148.  
     
  149.  
    }
  150.  
    }
  151.  
     
  152.  
    public interface WebSokcetCallback {
  153.  
    void onConnect();
  154.  
     
  155.  
    void onDisonnect();
  156.  
     
  157.  
    void onMessageData(String var1);
  158.  
    }
  159.  
     
  160.  
    private static class SingletonHolder {
  161.  
    private static final WebSocketHelper instance = new WebSocketHelper();
  162.  
     
  163.  
    private SingletonHolder() {
  164.  
    }
  165.  
    }
  166.  
    }
学新通

2、HttpURLConnectionUtil

  1.  
    import android.support.annotation.Nullable;
  2.  
    import android.util.Log;
  3.  
     
  4.  
    import java.io.BufferedReader;
  5.  
    import java.io.IOException;
  6.  
    import java.io.InputStream;
  7.  
    import java.io.InputStreamReader;
  8.  
    import java.io.OutputStream;
  9.  
    import java.net.HttpURLConnection;
  10.  
    import java.net.MalformedURLException;
  11.  
    import java.net.URL;
  12.  
     
  13.  
    /**
  14.  
    * author : jsxin
  15.  
    * e-mail : jsxin0816@163.com
  16.  
    * time : 2023/07/24
  17.  
    * desc :
  18.  
    */
  19.  
    public class HttpURLConnectionUtil {
  20.  
    public static final String TAG = HttpURLConnectionUtil.class.getSimpleName();
  21.  
     
  22.  
    public HttpURLConnectionUtil() {
  23.  
    }
  24.  
     
  25.  
    public static String doGet(String httpUrl) {
  26.  
    HttpURLConnection connection = null;
  27.  
    InputStream is = null;
  28.  
    BufferedReader br = null;
  29.  
    StringBuffer result = new StringBuffer();
  30.  
     
  31.  
    try {
  32.  
    URL url = new URL(httpUrl);
  33.  
    connection = (HttpURLConnection)url.openConnection();
  34.  
    connection.setRequestMethod("GET");
  35.  
    connection.setReadTimeout(15000);
  36.  
    connection.connect();
  37.  
    if (connection.getResponseCode() == 200) {
  38.  
    is = connection.getInputStream();
  39.  
    if (null != is) {
  40.  
    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  41.  
    String temp = null;
  42.  
     
  43.  
    while(null != (temp = br.readLine())) {
  44.  
    result.append(temp);
  45.  
    }
  46.  
    }
  47.  
    }
  48.  
    } catch (IOException var19) {
  49.  
    var19.printStackTrace();
  50.  
    } finally {
  51.  
    if (null != br) {
  52.  
    try {
  53.  
    br.close();
  54.  
    } catch (IOException var18) {
  55.  
    var18.printStackTrace();
  56.  
    }
  57.  
    }
  58.  
     
  59.  
    if (null != is) {
  60.  
    try {
  61.  
    is.close();
  62.  
    } catch (IOException var17) {
  63.  
    var17.printStackTrace();
  64.  
    }
  65.  
    }
  66.  
     
  67.  
    connection.disconnect();
  68.  
    }
  69.  
     
  70.  
    return result.toString();
  71.  
    }
  72.  
     
  73.  
    public static String doPost(String httpUrl, @Nullable String param) {
  74.  
    StringBuffer result = new StringBuffer();
  75.  
    HttpURLConnection connection = null;
  76.  
    OutputStream os = null;
  77.  
    InputStream is = null;
  78.  
    BufferedReader br = null;
  79.  
     
  80.  
    try {
  81.  
    URL url = new URL(httpUrl);
  82.  
    connection = (HttpURLConnection)url.openConnection();
  83.  
    connection.setRequestMethod("POST");
  84.  
    connection.setConnectTimeout(15000);
  85.  
    connection.setReadTimeout(15000);
  86.  
    connection.setUseCaches(false);
  87.  
    connection.setDoOutput(true);
  88.  
    connection.setDoInput(true);
  89.  
    connection.setRequestProperty("accept", "*/*");
  90.  
    connection.setRequestProperty("connection", "Keep-Alive");
  91.  
    connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
  92.  
    connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36)");
  93.  
    if (null != param && !param.equals("")) {
  94.  
    os = connection.getOutputStream();
  95.  
    os.write(param.getBytes("UTF-8"));
  96.  
    os.flush();
  97.  
    }
  98.  
     
  99.  
    int httpstatus = connection.getResponseCode();
  100.  
    Log.i(TAG, "http status :" httpstatus);
  101.  
    if (httpstatus == 200) {
  102.  
    is = connection.getInputStream();
  103.  
    if (null != is) {
  104.  
    br = new BufferedReader(new InputStreamReader(is, "utf-8"));
  105.  
    String temp = null;
  106.  
     
  107.  
    while(null != (temp = br.readLine())) {
  108.  
    result.append(temp);
  109.  
    result.append("\r\n");
  110.  
    }
  111.  
    }
  112.  
    }
  113.  
    } catch (MalformedURLException var30) {
  114.  
    var30.printStackTrace();
  115.  
    } catch (IOException var31) {
  116.  
    var31.printStackTrace();
  117.  
    } finally {
  118.  
    if (br != null) {
  119.  
    try {
  120.  
    br.close();
  121.  
    } catch (IOException var29) {
  122.  
    var29.printStackTrace();
  123.  
    }
  124.  
    }
  125.  
     
  126.  
    if (os != null) {
  127.  
    try {
  128.  
    os.close();
  129.  
    } catch (IOException var28) {
  130.  
    var28.printStackTrace();
  131.  
    }
  132.  
    }
  133.  
     
  134.  
    if (is != null) {
  135.  
    try {
  136.  
    is.close();
  137.  
    } catch (IOException var27) {
  138.  
    var27.printStackTrace();
  139.  
    }
  140.  
    }
  141.  
     
  142.  
    connection.disconnect();
  143.  
    }
  144.  
     
  145.  
    return result.toString();
  146.  
    }
  147.  
    }
学新通

3、Java-WebSocket-1.3.9.jar包(上传的是个压缩包,直接解压就能用)

链接: https://pan.百度.com/s/1ypl-bAsmERHn8D2XfdeZBQ?pwd=tcun 提取码: tcun

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbfgic
系列文章
更多 icon
同类精品
更多 icon
继续加载