帮助文档
专业提供香港服务器、香港云服务器、香港高防服务器租用、香港云主机、台湾服务器、美国服务器、美国云服务器vps租用、韩国高防服务器租用、新加坡服务器、日本服务器租用 一站式全球网络解决方案提供商!专业运营维护IDC数据中心,提供高质量的服务器托管,服务器机房租用,服务器机柜租用,IDC机房机柜租用等服务,稳定、安全、高性能的云端计算服务,实时满足您的多样性业务需求。 香港大带宽稳定可靠,高级工程师提供基于服务器硬件、操作系统、网络、应用环境、安全的免费技术支持。
服务器资讯 / 香港服务器租用 / 香港VPS租用 / 香港云服务器 / 美国服务器租用 / 台湾服务器租用 / 日本服务器租用 / 官方公告 / 帮助文档
Android 与服务器建立单向链接的SSE通讯机制
发布时间:2024-03-05 21:53:10   分类:帮助文档
Android 与服务器建立单向链接的SSE通讯机制

        前段时间公司有个项目需求,需要与后台服务器保持一个单向的长链接,也就是说只需要服务器向客户端发送指令,客户端做出相应操作就可以了,并不需要客户端向服务端发送什么请求。
网上找了很多资料,发现服务端用的SSE建立单向链接大多数是用到web端的,关于Android端的资料很少,经过一番探查,总算成功建立连接,以下代码为记录所用,觉得无用请划走即可。

1、必须确保我们的项目集成了相关依赖,这个链接机制肯定是基于okhttp的撒
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
implementation 'com.squareup.okhttp3:okhttp-sse:4.11.0'
2、这里我是自己写了一个工具类,需要的时候直接调用就可以了,虽然通篇就只调用了一次
直接贴出全部代码了
public class SSEClient {
private static SSEClient mInstance;
private SSEClient() {

}

public static synchronized SSEClient getInstance() {
if (mInstance == null) {
mInstance = new SSEClient();
}
return mInstance;
}

private EventSourceListener eventSourceListener;

//3
public void testOkhttpSSE(String userId) {
String url = baseUrl + userId;
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.DAYS)
.readTimeout(1, TimeUnit.DAYS)
.build();
Request request = new Request.Builder().url(url).build();
EventSource.Factory factory = EventSources.createFactory(okHttpClient);

eventSourceListener = new EventSourceListener() {
/
* {@inheritDoc}
*/
@Override
public void onOpen(final EventSource eventSource, final Response response) {
LogUtil.e("建立sse连接...","建立sse连接...");

}

/
* {@inheritDoc}
*/
@Override
public void onEvent(final EventSource eventSource, final String id, final String type, final String data) {
LogUtil.e("建立sse连接成功","建立sse连接成功" + data);
if (mSseConnectSucccesListener != null){
mSseConnectSucccesListener.getData(data);
}
}

/
* {@inheritDoc}
*/
@Override
public void onClosed(final EventSource eventSource) {
LogUtil.e("建立sse连接关闭","建立sse连接关闭");
//连接失败尝试重新连接
if (request != null && eventSourceListener != null){
LogUtil.e("SSEClient","连接异常,正在尝试重新连接...");
factory.newEventSource(request,eventSourceListener);
}else {
ToastUtils.showShort("连接异常,请稍后重试");
}
}

/
* {@inheritDoc}
*/
@Override
public void onFailure(final EventSource eventSource, final Throwable t, final Response response) {
// LogUtil.e("使用事件源时出现异常","连接服务端时出现异常-----" + response.message());
//连接失败尝试重新连接
if (request != null && eventSourceListener != null){
LogUtil.e("SSEClient","连接异常,正在尝试重新连接...");
factory.newEventSource(request,eventSourceListener);
}else {
ToastUtils.showShort("连接异常,请稍后重试");
}
}
};
//创建事件
factory.newEventSource(request, eventSourceListener);
//由于springboot test异步的,加下面代码卡住同步
// CountDownLatch countDownLatch = new CountDownLatch(1);
// try {
// countDownLatch.await();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

}



//建立监听回调
private SSEConnectSucccesListener mSseConnectSucccesListener;
public void getSSEMessage(SSEConnectSucccesListener sseConnectSucccesListener){
mSseConnectSucccesListener = sseConnectSucccesListener;
}

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







香港云服务器租用推荐
服务器租用资讯
·广东云服务有限公司怎么样
·广东云服务器怎么样
·广东锐讯网络有限公司怎么样
·广东佛山的蜗牛怎么那么大
·广东单位电话主机号怎么填写
·管家婆 花生壳怎么用
·官网域名过期要怎么办
·官网邮箱一般怎么命名
·官网网站被篡改怎么办
服务器租用推荐
·美国服务器租用
·台湾服务器租用
·香港云服务器租用
·香港裸金属服务器
·香港高防服务器租用
·香港服务器租用特价