Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions android/src/main/java/com/reactnativegrpc/GrpcModule.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.reactnativegrpc;

import android.util.Log;
import android.widget.Toast;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.UiThread;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
Expand All @@ -19,6 +23,7 @@

import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ConnectivityState;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
Expand All @@ -40,6 +45,8 @@ public class GrpcModule extends ReactContextBaseJavaModule {

private ManagedChannel managedChannel = null;

private boolean isUiLogEnabled = false;

public GrpcModule(ReactApplicationContext context) {
this.context = context;
}
Expand Down Expand Up @@ -332,9 +339,9 @@ private static String normalizePath(String path) {

private void handleOptionsChanged() {
if (this.managedChannel != null) {
this.managedChannel.resetConnectBackoff();
this.managedChannel.shutdown();
}

this.managedChannel = createManagedChannel();
}

Expand All @@ -359,4 +366,56 @@ private ManagedChannel createManagedChannel() {
managedChannel = channelBuilder.build();
return managedChannel;
}

@ReactMethod
public void resetConnection(final String message){
handleOptionsChanged();

showToast("resetConnection "+message);
}

@ReactMethod
public void onConnectionStateChange(){
if(null == managedChannel) return;

final ConnectivityState connectivityState = managedChannel.getState(true);
if(ConnectivityState.CONNECTING == connectivityState){
showToast("onConnectionState CONNECTING");
} else if(ConnectivityState.IDLE == connectivityState){
showToast("onConnectionState IDLE");
} else if(ConnectivityState.READY == connectivityState){
showToast("onConnectionState READY");
} else if(ConnectivityState.TRANSIENT_FAILURE == connectivityState){
showToast("onConnectionState TRANSIENT_FAILURE");
} else if(ConnectivityState.SHUTDOWN == connectivityState){
showToast("onConnectionState SHUTDOWN");
} else {
showToast("onConnectionState UNDEFINED");
}
if(ConnectivityState.TRANSIENT_FAILURE == connectivityState && managedChannel.isTerminated() || managedChannel.isShutdown()){
resetConnection("onConnectionStateChange");
}
}

@ReactMethod
public void enterIdle(){
if(null == managedChannel) return;

managedChannel.enterIdle();

showToast("enterIdle");
}

@ReactMethod
public void setUiLogEnabled(boolean isUiLogEnabled){
this.isUiLogEnabled = isUiLogEnabled;
}

@UiThread
private void showToast(final String message){
if(!isUiLogEnabled || null == context) return;

Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
Log.d("GRPC_MODULE",message);
}
}
57 changes: 56 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type GrpcType = {
setCompression(enable: boolean, compressorName: string, limit?: string): void;
setKeepalive(enabled: boolean, time: number, timeout: number): void;
setResponseSizeLimit(limitInBytes: number): void;
resetConnection(message: string):void;
setKeepAliveTime(keepAliveTime: number):void;
onConnectionStateChange():void;
setUiLogEnabled(enable:boolean):void;
enterIdle():void;
unaryCall(
id: number,
path: string,
Expand Down Expand Up @@ -187,7 +192,7 @@ export class GrpcClient {
compressorName: string,
limit?: number
): void {
if (Platform.OS !== 'android') {
if (!this.isAndroid()) {
return;
}
Grpc.setCompression(enable, compressorName, limit?.toString());
Expand All @@ -198,6 +203,49 @@ export class GrpcClient {
setResponseSizeLimit(limitInBytes: number): void {
Grpc.setResponseSizeLimit(limitInBytes);
}

/**
* resetConnection - only for Android
* @param message - to debug where we are calling 'resetConnection'
* */
resetConnection(message: string): void {
if (!this.isAndroid()) {
return;
}
Grpc.resetConnection(message);
}
/**
* setUiLogEnabled - only for Android
* @param enable - to debug where we are calling current grpc connection status
* in ui
* */
setUiLogEnabled(enable: boolean): void {
if (!this.isAndroid()) {
return;
}
Grpc.setUiLogEnabled(enable);
}
/**
* onConnectionStateChange - only for Android
* grpc connection different state
* * */
onConnectionStateChange(): void {
if (!this.isAndroid()) {
return;
}
Grpc.onConnectionStateChange();
}
/**
* enterIdle - only for Android
* set grpc connection state to idle
* * */
enterIdle(): void {
if (!this.isAndroid()) {
return;
}
Grpc.enterIdle();
}

unaryCall(
method: string,
data: Uint8Array,
Expand Down Expand Up @@ -291,6 +339,13 @@ export class GrpcClient {

return call;
}



private isAndroid(): Boolean {
return Platform.OS == 'android';
}

}

export { Grpc };