Skip to content

Commit d7ac973

Browse files
authored
Merge pull request #5 from pusher/fix-private-encrypted-subscription
Change subscribe function to consider private-encrypted channels
2 parents 0bb2db5 + 957dd71 commit d7ac973

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

android/src/main/java/com/pusherwebsocketreactnative/PusherWebsocketReactNativeModule.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
106106
@ReactMethod
107107
fun subscribe(channelName: String, promise: Promise) {
108108
val channel = when {
109-
channelName.startsWith("private-") -> pusher!!.subscribePrivate(channelName, this)
110109
channelName.startsWith("private-encrypted-") -> pusher!!.subscribePrivateEncrypted(
111110
channelName, this
112111
)
112+
channelName.startsWith("private-") -> pusher!!.subscribePrivate(channelName, this)
113113
channelName.startsWith("presence-") -> pusher!!.subscribePresence(
114114
channelName, this
115115
)
@@ -127,15 +127,18 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
127127

128128
@ReactMethod
129129
fun trigger(channelName: String, eventName: String, data: String, promise: Promise) {
130-
when {
131-
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName)
132-
.trigger(eventName, data)
133-
channelName.startsWith("private-encrypted-") -> throw Exception("It's not currently possible to send a message using private encrypted channels.")
134-
channelName.startsWith("presence-") -> pusher!!.getPresenceChannel(channelName)
135-
.trigger(eventName, data)
136-
else -> throw Exception("Messages can only be sent to private and presence channels.")
130+
try {
131+
when {
132+
channelName.startsWith("private-encrypted-") -> throw Exception("It's not currently possible to send a message using private encrypted channels.")
133+
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName).trigger(eventName, data)
134+
channelName.startsWith("presence-") -> pusher!!.getPresenceChannel(channelName)
135+
.trigger(eventName, data)
136+
else -> throw Exception("Messages can only be sent to private and presence channels.")
137+
}
138+
promise.resolve(null)
139+
} catch (e: Exception) {
140+
promise.reject(e)
137141
}
138-
promise.resolve(null)
139142
}
140143

141144
@ReactMethod

0 commit comments

Comments
 (0)