我正在使用heroku和android客户端的node.js web服务器进行web socket.io通信。
每当服务器消息发送给客户端Android时,我都想接收它,甚至关闭Android屏幕。
因此,我基本上是让服务,socket.on侦听器,线程和处理程序在安卓上。
还应用了partial_wake_lock,前台服务,发送通知,每5秒一次乒乓球.
当Android的屏幕打开时,我的系统运行良好。
但是在Android屏幕关闭大约30秒后,web连接将断开连接。
你能给我一些关于长期运行的网络套接字服务源代码的例子或者关于我的代码的一些解决方案吗?
谢谢你的阅读。
mainActivity
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "My:Tag");
wakeLock.acquire();
//apply wake_lock etc
(...)
Intent serviceIntent = new Intent(getApplicationContext(),CommunicationService.class);
startService(serviceIntent); //init service通讯服务(扩展服务)
@Override
public int onStartCommand(Intent intent, int flags, int startId){
//start foreground-service
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
Log.d("gd","entering");
notification =
new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("KD SERVICE TEST")
.setContentText("now koda testing" )
.setSmallIcon(R.drawable.ic_android_ex)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
if(webThread == null) {
Log.d("gd","webthread begin");
webThread = new WebThread(url, role, this.handler);
webThread.start();
}
return START_NOT_STICKY; //I tried STICKY, but nothing 类webThread扩展线程:构造函数并运行
在webThread.run中,线程总是每5秒发送一次'ping‘到服务器,当服务器得到'ping’时总是回答‘乒乓’。
在我的意图中,当没有'pong‘时,它的意思是通信= false,再次尝试socket.connect()。这个处理程序来自communicationService。
public WebThread(String get_url, int input_role, android.os.Handler handler){
try {
socket = IO.socket(get_url);
Log.d("gd","socket status: 0 " + socket.connected());
socket.connect();
socket.emit("join",role,"01");
} catch (URISyntaxException e) {
Log.d("gd", "web server enter failed");
e.printStackTrace();
}
web_listener_init();
this.handler = handler;
Log.d("gd","web thread created");
}
@Override
public void run(){
while(true){
if(isInterrupted())
return;
//when connection status is fine, correspond == true.
if(correspond ==false) {
socket.connect();
socket.emit("join", role, "01");
}
Message msg = Message.obtain();
msg.what = STATE_HEARTBEAT;
handler.sendMessage(msg);
correspond = false;
try {
Thread.sleep(5000);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}这是通信服务类中的处理程序。
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
case STATE_HEARTBEAT:
webThread.getSocket().emit("send_ping");
//sendNotification("ping");
break;
case STATE_WEB_MESSAGE_RECEIVED:
String webMessage = msg.obj.toString();
Log.d("gd", "handler received from web: " + webMessage);
if(webMessage.equals("pong")){
webThread.isCorrespond(); // make webThread 's 'correspond' = true
}
});我正在使用nkzawa Socket.io库。如果能给你一些建议,我会非常感激的。
发布于 2019-11-27 04:15:20
我解决了我的问题。突然断开不是从服务器/服务/乒乓。
来自安卓系统本身。
如果不将wake_lock(paritial_wake_lock)应用于服务类,
即使它的服务是前台,服务也不维护,或者定期通知。
我只在mainActivity中放置了wake锁,它在服务之前运行。
因此,在服务上应用wake_lock并放入清单!
https://stackoverflow.com/questions/59047411
复制相似问题