11package com.troplo.privateuploader.api
22
3- import android.app.NotificationManager
4- import android.content.Context.NOTIFICATION_SERVICE
5- import android.content.SharedPreferences
6- import android.preference.PreferenceManager
7- import androidx.core.app.NotificationCompat
8- import androidx.core.content.ContextCompat.getSystemService
9- import com.troplo.privateuploader.R
3+ import android.content.Context
104import io.socket.client.IO
5+ import io.socket.client.Manager
116import io.socket.client.Socket
12- import io.socket.emitter.Emitter
7+ import java.net.URI
138import java.net.URISyntaxException
14- import java.util.Collections.singletonMap
9+ import java.util.Collections
1510
1611
1712object SocketHandler {
18- lateinit var tpuSocket : Socket
13+ private const val SERVER_URL = " http://192.168.0.12:34582 " // Replace with your Socket.io server URL
1914
20- @Synchronized
21- fun setSocket (token : String? ) {
15+ private var socket: Socket ? = null
16+
17+ fun initializeSocket (token : String ) {
2218 try {
23- val options = IO .Options .builder()
24- .setAuth(singletonMap(" token" , token))
25- .build()
26- tpuSocket = IO .socket(" http://192.168.0.12:34582" , options)
27- println (" Socket set" )
19+ val options = IO .Options ()
20+ options.forceNew = true
21+ options.reconnection = true
22+ options.auth = Collections .singletonMap(" token" , token)
23+ socket = IO .socket(SERVER_URL , options)
24+ if (socket != null ) {
25+ socket?.open()
26+ socket?.on(Socket .EVENT_CONNECT ) {
27+ println (" Socket connected ${socket?.isActive} " )
28+ }
29+ socket?.on(Socket .EVENT_DISCONNECT ) {
30+ println (" Socket disconnected ${socket?.isActive} " )
31+ }
32+ socket?.on(Socket .EVENT_CONNECT_ERROR ) {
33+ println (" Socket connect error ${socket?.isActive} " )
34+ }
35+ // socket on any other events
36+ socket?.on(" message" ) {
37+ println (" Socket $it " )
38+ }
39+ println (" Socket connected ${socket?.isActive} " )
40+ } else {
41+ println (" Socket is null" )
42+ }
2843 } catch (e: URISyntaxException ) {
2944 e.printStackTrace()
3045 }
3146 }
3247
33- @Synchronized
34- fun getSocket (): Socket {
35- return tpuSocket
36- }
37-
38- @Synchronized
39- fun establishConnection () {
40- tpuSocket.connect()
41- }
42-
43- @Synchronized
44- fun closeConnection () {
45- tpuSocket.disconnect()
48+ fun getSocket (): Socket ? {
49+ return socket
4650 }
4751
48- @Synchronized
49- fun listeners () {
50- tpuSocket.on(" message" , Emitter .Listener { args->
51- /* NotificationCompat.Builder(this, "communications")
52- .setStyle(NotificationCompat.MessagingStyle("Me")
53- .setConversationTitle(TpuFunctions.getChatName(args[0].chat as Chat?))
54- .build()
55- )*/
56- println (args)
57- })
58-
59- tpuSocket.on(Socket .EVENT_CONNECT ) {
60- println (" Connected to TPU Server" )
61- }
62- tpuSocket.on(Socket .EVENT_DISCONNECT ) {
63- println (" Disconnected from TPU Server" )
64- }
65- tpuSocket.on(" echo" ) { args ->
66- println (args[0 ])
67- }
52+ fun closeSocket () {
53+ socket?.disconnect()
54+ socket = null
6855 }
6956}
0 commit comments