【发布时间】:2020-03-21 15:56:44
【问题描述】:
我正在使用 heroku 的 node.js web 服务器和 android 客户端进行 web socket.io 通信
我想在发送到客户端 Android 时接收服务器消息,即使 Android 屏幕关闭
所以我基本上是在 Android 上制作 Service 、 socket.on listener 、 thread & handler 。
也确实应用了 partial_wake_lock、前台服务、发送通知、每 5 秒乒乓球...
Android 屏幕开启时我的系统运行良好。
但在 Android 屏幕关闭后大约 30 秒,网络连接将断开
你能给我一些关于长期运行的网络套接字服务源代码的例子或一些关于我的代码的解决方案吗?
感谢您的阅读。
主活动
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”
在我的意图中,当没有'pong'时,表示对应= false,再次尝试socket.connect()。 这个处理程序来自通信服务。
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 库。非常感谢您提供一些建议。
【问题讨论】:
标签: android node.js heroku websocket android-webservice