Right now my app has one single activity which has full support of WS actions with listeners and creating new instance of webSocket. I would like to decompose this to one activity which will be like a holder for several fragments.
At the beginning I would like to create webSocket in the activity and then work with created webSocket on fragments. I work with okHttp3 websockets. In general I can for example create one single viewModel where I will initialize websocket, but I need to pass also listener during WS creating:
val loggingInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder()
.addInterceptor(AuthToken(this))
.addInterceptor(loggingInterceptor)
.build()
val request = Request.Builder()
.url(
BuildConfig.CHAT_URL + UtilsClass.userInfo.applicant?.id + "/"
)
.addHeader("Sec-WebSocket-Protocol","android")
.build()
val ws = client.newWebSocket(request, listener) <---- here
on the other hand I need to have separate listeners for separate fragments. The call of .newWebSocket(... will create a new instance of webSocket what is not good for me. But when I will have one single shared webSocket listener I will need to handle from which fragment I wait info. Also maybe I decided to do a wrong thing which won't be good.