首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Android 与服务器建立单向链接的SSE通讯机制

  • 24-03-18 03:09
  • 3683
  • 10145
blog.csdn.net

        前段时间公司有个项目需求,需要与后台服务器保持一个单向的长链接,也就是说只需要服务器向客户端发送指令,客户端做出相应操作就可以了,并不需要客户端向服务端发送什么请求。

网上找了很多资料,发现服务端用的SSE建立单向链接大多数是用到web端的,关于Android端的资料很少,经过一番探查,总算成功建立连接,以下代码为记录所用,觉得无用请划走即可。

1、必须确保我们的项目集成了相关依赖,这个链接机制肯定是基于okhttp的撒

  1. implementation 'com.squareup.okhttp3:okhttp:4.11.0'
  2. implementation 'com.squareup.okhttp3:okhttp-sse:4.11.0'

2、这里我是自己写了一个工具类,需要的时候直接调用就可以了,虽然通篇就只调用了一次

直接贴出全部代码了

  1. public class SSEClient {
  2. private static SSEClient mInstance;
  3. private SSEClient() {
  4. }
  5. public static synchronized SSEClient getInstance() {
  6. if (mInstance == null) {
  7. mInstance = new SSEClient();
  8. }
  9. return mInstance;
  10. }
  11. private EventSourceListener eventSourceListener;
  12. //3
  13. public void testOkhttpSSE(String userId) {
  14. String url = baseUrl + userId;
  15. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  16. .connectTimeout(1, TimeUnit.DAYS)
  17. .readTimeout(1, TimeUnit.DAYS)
  18. .build();
  19. Request request = new Request.Builder().url(url).build();
  20. EventSource.Factory factory = EventSources.createFactory(okHttpClient);
  21. eventSourceListener = new EventSourceListener() {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. @Override
  26. public void onOpen(final EventSource eventSource, final Response response) {
  27. LogUtil.e("建立sse连接...","建立sse连接...");
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. @Override
  33. public void onEvent(final EventSource eventSource, final String id, final String type, final String data) {
  34. LogUtil.e("建立sse连接成功","建立sse连接成功" + data);
  35. if (mSseConnectSucccesListener != null){
  36. mSseConnectSucccesListener.getData(data);
  37. }
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. @Override
  43. public void onClosed(final EventSource eventSource) {
  44. LogUtil.e("建立sse连接关闭","建立sse连接关闭");
  45. //连接失败尝试重新连接
  46. if (request != null && eventSourceListener != null){
  47. LogUtil.e("SSEClient","连接异常,正在尝试重新连接...");
  48. factory.newEventSource(request,eventSourceListener);
  49. }else {
  50. ToastUtils.showShort("连接异常,请稍后重试");
  51. }
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. @Override
  57. public void onFailure(final EventSource eventSource, final Throwable t, final Response response) {
  58. // LogUtil.e("使用事件源时出现异常","连接服务端时出现异常-----" + response.message());
  59. //连接失败尝试重新连接
  60. if (request != null && eventSourceListener != null){
  61. LogUtil.e("SSEClient","连接异常,正在尝试重新连接...");
  62. factory.newEventSource(request,eventSourceListener);
  63. }else {
  64. ToastUtils.showShort("连接异常,请稍后重试");
  65. }
  66. }
  67. };
  68. //创建事件
  69. factory.newEventSource(request, eventSourceListener);
  70. //由于springboot test异步的,加下面代码卡住同步
  71. // CountDownLatch countDownLatch = new CountDownLatch(1);
  72. // try {
  73. // countDownLatch.await();
  74. // } catch (InterruptedException e) {
  75. // e.printStackTrace();
  76. // }
  77. }
  78. //建立监听回调
  79. private SSEConnectSucccesListener mSseConnectSucccesListener;
  80. public void getSSEMessage(SSEConnectSucccesListener sseConnectSucccesListener){
  81. mSseConnectSucccesListener = sseConnectSucccesListener;
  82. }
  83. public interface SSEConnectSucccesListener{
  84. void getData(String data);
  85. }

上面这段代码写的是一个单例模式,还是那句话,通篇只使用一次。里面的baseUrl是自己的服务器地址,copy的时候自己添加就可以了,下面的监听会把你需要的数据传递出去。

注:本文转载自blog.csdn.net的愚933的文章"https://blog.csdn.net/weixin_72827485/article/details/131931990"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

121
服务器
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top