当前位置:   article > 正文

基于okhttp3.0的WebSocket的简易实践_okhttp.jar websocket

okhttp.jar websocket

工作日志记录:工作中有个需求就是实现实时推送,实时推送基本上都是要基于Socket通讯技术的,这里使用okhttp3.0提供的WebSocket框架,现记录下来便于后面使用时更快;

具体代码如下:

  1. package com.haocang.waterpro.mango;
  2. import android.app.Service;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Handler;
  6. import android.os.IBinder;
  7. import android.os.Message;
  8. import androidx.annotation.Nullable;
  9. import com.haocang.commonui.route.MangoRouteURL;
  10. import com.haocang.core.utils.StringUtils;
  11. import com.haocang.core.utils.ToastUtils;
  12. import com.haocang.core.utils.cache.CacheHelper;
  13. import com.haocang.waterpro.utils.NotificationUtils;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import java.lang.ref.WeakReference;
  17. import okhttp3.OkHttpClient;
  18. import okhttp3.Request;
  19. import okhttp3.Response;
  20. import okhttp3.WebSocketListener;
  21. import okio.ByteString;
  22. /**
  23. * 文件描述:WebSocket获取推送信息
  24. * 作者:徐干稳
  25. * 创建时间:2019/10/30
  26. * 更改时间:2019/10/30
  27. * 版本号:1.0
  28. */
  29. public class LocalWebSocketService extends Service {
  30. private MyHandler handler;
  31. @Nullable
  32. @Override
  33. public IBinder onBind(Intent intent) {
  34. return null;
  35. }
  36. @Override
  37. public int onStartCommand(Intent intent, int flags, int startId) {
  38. handler = new MyHandler(new WeakReference<Context>(this).get());
  39. String url = "ws://172.19.1.130:18432/web-socket/" +CacheHelper.get("login_name");
  40. start(url);
  41. return START_STICKY;
  42. }
  43. @Override
  44. public boolean stopService(Intent name) {
  45. return super.stopService(name);
  46. }
  47. private void start(String wsUrl) {
  48. Request request = new Request.Builder().url(wsUrl).build();
  49. OkHttpClient client = new OkHttpClient();
  50. client.newWebSocket(request, new WebSocketListener() {
  51. @Override
  52. public void onOpen(okhttp3.WebSocket webSocket, Response response) {
  53. super.onOpen(webSocket, response);
  54. // handler.obtainMessage(0, "WebSocket开启").sendToTarget();
  55. // webSocket.send("收到消息了吗");
  56. }
  57. @Override
  58. public void onMessage(okhttp3.WebSocket webSocket, String text) {
  59. super.onMessage(webSocket, text);
  60. handler.obtainMessage(1, text).sendToTarget();
  61. }
  62. @Override
  63. public void onMessage(okhttp3.WebSocket webSocket, ByteString bytes) {
  64. super.onMessage(webSocket, bytes);
  65. }
  66. @Override
  67. public void onClosing(okhttp3.WebSocket webSocket, int code, String reason) {
  68. super.onClosing(webSocket, code, reason);
  69. }
  70. @Override
  71. public void onClosed(okhttp3.WebSocket webSocket, int code, String reason) {
  72. super.onClosed(webSocket, code, reason);
  73. }
  74. @Override
  75. public void onFailure(okhttp3.WebSocket webSocket, Throwable t, Response response) {
  76. super.onFailure(webSocket, t, response);
  77. handler.obtainMessage(4, "WebSocket连接已关闭").sendToTarget();
  78. }
  79. });
  80. client.dispatcher().executorService().shutdown();
  81. }
  82. public class MyHandler extends Handler {
  83. private Context context;
  84. public MyHandler(Context context) {
  85. this.context = context;
  86. }
  87. @Override
  88. public void handleMessage(Message msg) {
  89. super.handleMessage(msg);
  90. if (null != context) {
  91. switch (msg.what) {
  92. case 1:
  93. String json = (String) msg.obj;
  94. if (!StringUtils.isEmpty(json)) {
  95. try {
  96. JSONObject jsonObject = new JSONObject(json);
  97. }
  98. } catch (JSONException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. break;
  103. case 4:
  104. ToastUtils.showShort((String) msg.obj);
  105. break;
  106. case 0:
  107. ToastUtils.showShort((String) msg.obj);
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/261958
推荐阅读
相关标签
  

闽ICP备14008679号