在我的应用程序中,我启动了一个
WebSocketWorker
任务,每15分钟定期运行一次。正如名称所示,它包含一个WebSocket用于在后台监听套接字:// MainApplication.kt
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
val work = PeriodicWorkRequestBuilder<WebSocketWorker>(15, TimeUnit.MINUTES).build()
workManager.enqueueUniquePeriodicWork("UniqueWebSocketWorker", ExistingPeriodicWorkPolicy.KEEP, work)
}
WebSocketWorker
包含如下逻辑:
@HiltWorker
class WebSocketWorker @AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
inner class MyWebSocketListener : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
Timber.d("The message sent is %s", text)
// do sth. with the message
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
t.localizedMessage?.let { Timber.e("onFailure: %s", it) }
response?.message?.let { Timber.e("onFailure: %s", it) }
}
}
override suspend fun doWork(): Result {
try{
// code to be executed
val request = Request.Builder().url("ws://***.***.**.***:8000/ws/chat/lobby/").build()
val myWebSocketListener = MyWebSocketListener()
val client = OkHttpClient()
client.newWebSocket(request, myWebSocketListener)
return Result.success()
}
catch (throwable:Throwable){
Timber.e("There is a failure")
Timber.e("throwable.localizedMessage: %s", throwable.localizedMessage)
// clean up and log
return Result.failure()
}
}
}
正如您所看到的,在Worker类中设置WebSocket一切都很好。监听套接字起作用。
现在,我还想向我的应用程序添加“发送消息”的功能。我该如何重用在WebSocketWorker
中创建的websocket?我能将输入数据传递给后台运行的WebSocketWorker
吗?
假设我有一个EditText
用于输入消息,以及一个带有setOnClickListener的Button
来发送消息,就像这样:
binding.sendButton.setOnClickListener {
// get message
val message = binding.chatMessageEditText.text.toString()
// check if not empty
if(message.isNotEmpty()) {
// HOW CAN I REUSE THE WEBSOCKET RUNNING PERIODICALLY IN THE BACKGROUND?
// CAN I PASS THE MESSAGE TO THAT WEBSOCKET ?
// OR SHOULD I CREATE A DIFFERENT WORKER FOR SENDING MESSAGES (e.g.: a OneTimeRequest<SendMessageWorker> for sending messages ?
}
}
从文档中了解到,需要构建数据对象以传递输入等内容,但没有例子展示如何向后台定期运行的工作程序传递输入。
(译者注:建议将“Data objects”翻译为“数据对象”,“worker”翻译为“工作者”或“任务处理器”)