Skip to content

Commit 00ea9d6

Browse files
committed
feat: store socket instances on hash maps
1 parent afd5c8f commit 00ea9d6

File tree

2 files changed

+66
-39
lines changed

2 files changed

+66
-39
lines changed

android/src/main/java/com/reactnativesocketio/SocketIoModule.kt

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import org.json.JSONObject
1313
import java.net.URISyntaxException
1414
import java.util.*
1515
import kotlin.collections.ArrayList
16+
import kotlin.collections.HashMap
1617
import kotlin.math.round
1718

1819

1920
@ReactModule(name = SocketIoModule.NAME, hasConstants = false)
2021
class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
2122
companion object { const val NAME = "RNSocketIO" }
2223

23-
private var mSocket: Socket? = null
24+
private var mSocketList = HashMap<String, Socket>()
2425

2526
private val callbackRegisters: ArrayList<CallbackRegister> = ArrayList()
2627

@@ -37,7 +38,11 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
3738
override fun onCatalystInstanceDestroy() {
3839
super.onCatalystInstanceDestroy()
3940
// clean register to avoid storing unnecessarily data to memory
40-
callbackRegisters.forEach { mSocket?.off(it.eventName, it.onEventListener) }
41+
callbackRegisters.forEach {
42+
for(key in mSocketList.keys) {
43+
getSocketInstance(key)?.off(it.eventName, it.onEventListener)
44+
}
45+
}
4146

4247
callbackRegisters.clear()
4348
}
@@ -61,6 +66,10 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
6166
return callbackRegister
6267
}
6368

69+
private fun getSocketInstance(path: String): Socket? {
70+
return mSocketList[path]
71+
}
72+
6473
@ReactMethod
6574
fun initialize(url: String, options: ReadableMap, callback: Callback) {
6675
try {
@@ -108,14 +117,12 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
108117
}
109118

110119
if(options.hasKey("path")) {
111-
options.getString("path").let { socketOptions.path = it }
120+
options.getString("path")?.let {
121+
socketOptions.path = it
122+
}
112123
}
113124

114-
callbackRegisters.clear()
115-
116-
mSocket?.off()
117-
118-
mSocket = IO.socket(url, socketOptions)
125+
mSocketList[socketOptions.path] = IO.socket(url, socketOptions)
119126

120127
callback.invoke(null)
121128
} catch (e: URISyntaxException) {
@@ -124,17 +131,23 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
124131
}
125132

126133
@ReactMethod
127-
fun connect() {
134+
fun connect(path: String) {
135+
val mSocket = getSocketInstance(path)
136+
128137
mSocket?.connect()
129138
}
130139

131140
@ReactMethod
132-
fun disconnect() {
141+
fun disconnect(path: String) {
142+
val mSocket = getSocketInstance(path)
143+
133144
mSocket?.disconnect()
134145
}
135146

136147
@ReactMethod
137-
fun emit(eventName: String, options: ReadableMap) {
148+
fun emit(path: String, eventName: String, options: ReadableMap) {
149+
val mSocket = getSocketInstance(path)
150+
138151
if(options.hasKey("data")) {
139152

140153
when(options.getType("data")) {
@@ -169,7 +182,8 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
169182
}
170183

171184
@ReactMethod
172-
fun on(eventName: String, callback: Callback) {
185+
fun on(path: String, eventName: String, callback: Callback) {
186+
val mSocket = getSocketInstance(path)
173187
val callbackRegister = buildCallbackRegister(eventName)
174188

175189
callback.invoke(eventName, callbackRegister.uniqueID)
@@ -178,7 +192,8 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
178192
}
179193

180194
@ReactMethod
181-
fun once(eventName: String, callback: Callback) {
195+
fun once(path: String, eventName: String, callback: Callback) {
196+
val mSocket = getSocketInstance(path)
182197
val callbackRegister = buildCallbackRegister(eventName)
183198

184199
callback.invoke(eventName, callbackRegister.uniqueID)
@@ -187,7 +202,8 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
187202
}
188203

189204
@ReactMethod
190-
fun off(eventName: String, uniqueID: String) {
205+
fun off(path: String, eventName: String, uniqueID: String) {
206+
val mSocket = getSocketInstance(path)
191207
val index = callbackRegisters.indexOfFirst { it.uniqueID == uniqueID }
192208

193209
if(index < 0) return
@@ -198,22 +214,26 @@ class SocketIoModule(private val reactContext: ReactApplicationContext) : ReactC
198214
}
199215

200216
@ReactMethod
201-
fun connected(promise: Promise) {
202-
promise.resolve(connectedSync())
217+
fun connected(path: String, promise: Promise) {
218+
promise.resolve(connectedSync(path))
203219
}
204220

205221
@ReactMethod
206-
fun getId(promise: Promise) {
207-
promise.resolve(getIdSync())
222+
fun getId(path: String, promise: Promise) {
223+
promise.resolve(getIdSync(path))
208224
}
209225

210226
@ReactMethod(isBlockingSynchronousMethod = true)
211-
fun connectedSync(): Boolean {
227+
fun connectedSync(path: String): Boolean {
228+
val mSocket = getSocketInstance(path)
229+
212230
return mSocket?.connected() ?: false
213231
}
214232

215233
@ReactMethod(isBlockingSynchronousMethod = true)
216-
fun getIdSync(): String? {
234+
fun getIdSync(path: String): String? {
235+
val mSocket = getSocketInstance(path)
236+
217237
return mSocket?.id()
218238
}
219239
}

src/index.tsx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export type SocketIOModuleType = {
3232
options: SocketIOOptions,
3333
callbackResponse: SocketCallbackResponse
3434
): void;
35-
connect(): void;
36-
disconnect(): void;
37-
emit(eventName: string, options: SocketIOEventData): void;
38-
on(eventName: string, callback: Function): void;
39-
once(eventName: string, callback: Function): void;
40-
off(eventName: string, uniqueID: string): void;
41-
connected(callback: SocketConnectedCallback): void;
42-
connectedSync(): boolean;
43-
getId(callback: SocketIdCallback): void;
44-
getIdSync(): string | null;
35+
connect(path: string): void;
36+
disconnect(path: string): void;
37+
emit(path: string, eventName: string, options: SocketIOEventData): void;
38+
on(path: string, eventName: string, callback: Function): void;
39+
once(path: string, eventName: string, callback: Function): void;
40+
off(path: string, eventName: string, uniqueID: string): void;
41+
connected(path: string, callback: SocketConnectedCallback): void;
42+
connectedSync(path: string): boolean;
43+
getId(path: string, callback: SocketIdCallback): void;
44+
getIdSync(path: string): string | null;
4545
};
4646

4747
class SocketIO {
@@ -54,10 +54,15 @@ class SocketIO {
5454
};
5555
};
5656
eventEmitter: NativeEventEmitter;
57+
path: string;
58+
options: SocketIOOptions;
5759

5860
constructor(url: string, options?: SocketIOOptions) {
5961
this.SocketIOModule = NativeModules.RNSocketIO;
6062
this.SocketIOCallbacksList = {};
63+
this.path = options?.path || '/socket.io';
64+
console.log(this.path);
65+
this.options = { ...options, path: this.path };
6166

6267
this.eventEmitter = new NativeEventEmitter(NativeModules.SocketIo);
6368

@@ -72,7 +77,7 @@ class SocketIO {
7277

7378
this.SocketIOModule.initialize(
7479
url,
75-
options ?? {},
80+
this.options,
7681
this._callCallbackResponse
7782
);
7883
}
@@ -87,14 +92,14 @@ class SocketIO {
8792
* Open socket connection.
8893
*/
8994
connect() {
90-
this.SocketIOModule.connect();
95+
this.SocketIOModule.connect(this.path);
9196
}
9297

9398
/**
9499
* Close socket connection.
95100
*/
96101
disconnect() {
97-
this.SocketIOModule.disconnect();
102+
this.SocketIOModule.disconnect(this.path);
98103
}
99104

100105
/**
@@ -103,7 +108,7 @@ class SocketIO {
103108
* @param data Data to send on socket event.
104109
*/
105110
emit(eventName: string, data?: SocketIOEventData) {
106-
this.SocketIOModule.emit(eventName, { data });
111+
this.SocketIOModule.emit(this.path, eventName, { data });
107112
}
108113

109114
/**
@@ -113,6 +118,7 @@ class SocketIO {
113118
*/
114119
on(eventName: string, callback: Function) {
115120
this.SocketIOModule.on(
121+
this.path,
116122
eventName,
117123
(nativeEventName: string, uniqueID: string) => {
118124
this.SocketIOCallbacksList[uniqueID] = {
@@ -131,6 +137,7 @@ class SocketIO {
131137
*/
132138
once(eventName: string, callback: Function) {
133139
this.SocketIOModule.once(
140+
this.path,
134141
eventName,
135142
(nativeEventName: string, uniqueID: string) => {
136143
this.SocketIOCallbacksList[uniqueID] = {
@@ -156,7 +163,7 @@ class SocketIO {
156163
if (listItem.eventName === eventName && listItem.callback === callback) {
157164
keyToDelete = listItem.uniqueID;
158165

159-
this.SocketIOModule.off(eventName, keyToDelete);
166+
this.SocketIOModule.off(this.path, eventName, keyToDelete);
160167

161168
break;
162169
}
@@ -172,31 +179,31 @@ class SocketIO {
172179
* @param callback Callback with connection status of socket.
173180
*/
174181
connected(callback: SocketConnectedCallback) {
175-
this.SocketIOModule.connected(callback);
182+
this.SocketIOModule.connected(this.path, callback);
176183
}
177184

178185
/**
179186
* Get id of socket.
180187
* @param callback Callback with id of socket.
181188
*/
182189
getId(callback: SocketIdCallback) {
183-
this.SocketIOModule.getId(callback);
190+
this.SocketIOModule.getId(this.path, callback);
184191
}
185192

186193
/**
187194
* Get connection status of socket.
188195
* Warning: this method are synchronous blocking UI, use it carefully.
189196
*/
190197
connectedSync() {
191-
return this.SocketIOModule.connectedSync();
198+
return this.SocketIOModule.connectedSync(this.path);
192199
}
193200

194201
/**
195202
* Get id of socket.
196203
* Warning: this method are synchronous blocking UI, use it carefully.
197204
*/
198205
getIdSync() {
199-
return this.SocketIOModule.getIdSync();
206+
return this.SocketIOModule.getIdSync(this.path);
200207
}
201208

202209
static serializeQuery(object: any) {

0 commit comments

Comments
 (0)