1
1
package com.troplo.privateuploader.api
2
2
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
10
4
import io.socket.client.IO
5
+ import io.socket.client.Manager
11
6
import io.socket.client.Socket
12
- import io.socket.emitter.Emitter
7
+ import java.net.URI
13
8
import java.net.URISyntaxException
14
- import java.util.Collections.singletonMap
9
+ import java.util.Collections
15
10
16
11
17
12
object 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
19
14
20
- @Synchronized
21
- fun setSocket (token : String? ) {
15
+ private var socket: Socket ? = null
16
+
17
+ fun initializeSocket (token : String ) {
22
18
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
+ }
28
43
} catch (e: URISyntaxException ) {
29
44
e.printStackTrace()
30
45
}
31
46
}
32
47
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
46
50
}
47
51
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
68
55
}
69
56
}
0 commit comments