Skip to content

Commit

Permalink
Merge pull request #12 from youngmonkeys/fix-ping-schedule-issue
Browse files Browse the repository at this point in the history
fix: ping schedule issue
  • Loading branch information
tvd12 authored Sep 10, 2021
2 parents e93ca64 + 47fc3dc commit e2799a4
Show file tree
Hide file tree
Showing 32 changed files with 216 additions and 26 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ repositories {

dependencies {
//noinspection GradleDynamicVersion
implementation "com.tvd12:ezyfox-server-android-client:1.0.1"
implementation "com.tvd12:ezyfox-server-android-client:1.0.2"
implementation "com.facebook.react:react-native:+" // From node_modules
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.tvd12.ezyfoxserver.client.EzyClient;
import com.tvd12.ezyfoxserver.client.config.EzyPingConfig;
import com.tvd12.ezyfoxserver.client.react.EzyMethodNames;
import com.tvd12.ezyfoxserver.client.setup.EzySetup;
import com.tvd12.ezyfoxserver.client.config.EzyClientConfig;
Expand All @@ -20,6 +21,8 @@
import com.tvd12.ezyfoxserver.client.handler.EzyEventHandler;
import com.tvd12.ezyfoxserver.client.react.serializer.EzyNativeSerializers;

import java.util.Map;

/**
* Created by tavandung12 on 10/24/18.
*/
Expand Down Expand Up @@ -62,6 +65,14 @@ private EzyClientConfig newConfig(ReadableMap params) {
configBuilder.enableSSL(params.getBoolean("enableSSL"));
if(params.hasKey("enableDebug"))
configBuilder.enableDebug(params.getBoolean("enableDebug"));
if(params.hasKey("ping")) {
ReadableMap ping = params.getMap("ping");
EzyPingConfig.Builder pingConfigBuilder = configBuilder.pingConfigBuilder();
if(ping.hasKey("pingPeriod"))
pingConfigBuilder.pingPeriod(ping.getInt("pingPeriod"));
if(ping.hasKey("maxLostPingCount"))
pingConfigBuilder.maxLostPingCount(ping.getInt("maxLostPingCount"));
}
if(params.hasKey("reconnect")) {
ReadableMap reconnect = params.getMap("reconnect");
EzyReconnectConfig.Builder reconnectConfigBuilder = configBuilder.reconnectConfigBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.tvd12.ezyfoxserver.client.config.EzyClientConfig;
import com.tvd12.ezyfoxserver.client.config.EzyPingConfig;
import com.tvd12.ezyfoxserver.client.config.EzyReconnectConfig;

/**
Expand All @@ -16,12 +17,20 @@ public ReadableMap serialize(EzyClientConfig config) {
WritableMap map = Arguments.createMap();
map.putString("clientName", config.getClientName());
map.putString("zoneName", config.getZoneName());
WritableMap reconnectMap = Arguments.createMap();

EzyPingConfig pingConfig = config.getPing();
WritableMap pingMap = Arguments.createMap();
pingMap.putInt("maxLostPingCount", pingConfig.getMaxLostPingCount());
pingMap.putInt("pingPeriod", (int)pingConfig.getPingPeriod());
map.putMap("ping", pingMap);

EzyReconnectConfig reconnectConfig = config.getReconnect();
WritableMap reconnectMap = Arguments.createMap();
reconnectMap.putInt("maxReconnectCount", reconnectConfig.getMaxReconnectCount());
reconnectMap.putInt("reconnectPeriod", reconnectConfig.getReconnectPeriod());
reconnectMap.putBoolean("enable", reconnectConfig.isEnable());
map.putMap("reconnect", reconnectMap);

return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public WritableMap apply(EzyEvent event) {
public WritableMap apply(EzyEvent event) {
EzyConnectionFailureEvent mevent = (EzyConnectionFailureEvent)event;
WritableMap map = Arguments.createMap();
map.putString("reason", mevent.getReason().toString());
map.putInt("reason", mevent.getReason().getId());
return map;
}
});
Expand All @@ -61,9 +61,7 @@ public WritableMap apply(EzyEvent event) {
public WritableMap apply(EzyEvent event) {
EzyDisconnectionEvent mevent = (EzyDisconnectionEvent)event;
WritableMap map = Arguments.createMap();
int reason = mevent.getReason();
String reasonName = EzyDisconnectReasons.getDisconnectReasonName(reason);
map.putString("reason", reasonName);
map.putInt("reason", mevent.getReason());
return map;
}
});
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint ."
},
"dependencies": {
"ezyfox-server-react-native-client": "^1.0.0-beta5",
"ezyfox-server-react-native-client": "^1.0.1-beta2",
"mvc-es6": "^1.1.1",
"react": "17.0.1",
"react-native": "0.64.2"
Expand Down
25 changes: 25 additions & 0 deletions example/socket/SocketProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class SocketProxy {
config.clientName = ZONE_NAME;
config.enableSSL = true;
config.enableDebug = true;
config.ping.pingPeriod = 1000;
config.ping.maxLostPingCount = 3;
config.reconnect.reconnectPeriod = 1000;
config.reconnect.maxReconnectCount = 10;
let clients = Ezy.Clients.getInstance();
clients.newDefaultClient(config, client => {
this.doSetup(client);
Expand Down Expand Up @@ -68,12 +72,33 @@ class SocketProxy {
routerController.updateViews('change', 'login');
};

let lostPingHandler = {};
lostPingHandler.handle = function (event) {
console.log('lost ping: ' + JSON.stringify(event));
};

let connectionFailureHandler = new Ezy.ConnectionFailureHandler();
connectionFailureHandler.preHandle = function (event) {
console.log('connection failed: ' + JSON.stringify(event));
};

connectionFailureHandler.onConnectionFailed = function (event) {
console.log(
'disconnected due to connection failed: ' + JSON.stringify(event),
);
};

let setup = client.setup;
setup.addEventHandler(
Ezy.EventType.CONNECTION_SUCCESS,
connectionSuccessHandler,
);
setup.addEventHandler(
Ezy.EventType.CONNECTION_FAILURE,
connectionFailureHandler,
);
setup.addEventHandler(Ezy.EventType.DISCONNECTION, disconnectionHandler);
setup.addEventHandler(Ezy.EventType.LOST_PING, lostPingHandler);
setup.addDataHandler(Ezy.Command.HANDSHAKE, handshakeHandler);
setup.addDataHandler(Ezy.Command.LOGIN, loginSuccessHandler);
setup.addDataHandler(Ezy.Command.LOGIN_ERROR, loginErrorHandler);
Expand Down
12 changes: 12 additions & 0 deletions ios/EzyClient/proxy/EzyMethodProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,23 @@
EzyClientConfig* config = EzyClientConfig::create();
NSString* clientName = [params valueForKey:@"clientName"];
NSString* zoneName = [params valueForKey:@"zoneName"];
NSDictionary* ping = [params valueForKey:@"ping"];
NSDictionary* reconnect = [params valueForKey:@"reconnect"];
if(clientName)
config->setClientName([clientName UTF8String]);
if(zoneName)
config->setZoneName([zoneName UTF8String]);
if(ping) {
NSNumber* pingPeriod = [ping objectForKey:@"pingPeriod"];
NSNumber* maxLostPingCount = [ping objectForKey:@"maxLostPingCount"];
EzyPingConfig* pingConfig = config->getPing();
if(pingPeriod) {
pingConfig->setPingPeriod((int)[pingPeriod integerValue]);
}
if(maxLostPingCount) {
pingConfig->setMaxLostPingCount((int)[maxLostPingCount integerValue]);
}
}
if(reconnect) {
NSNumber* enable = [reconnect objectForKey:@"enable"];
NSNumber* reconnectPeriod = [reconnect objectForKey:@"reconnectPeriod"];
Expand Down
10 changes: 9 additions & 1 deletion ios/EzyClient/serializer/EzyClientConfigSerializer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ - (NSDictionary *)serialize:(void *)value {
NSDictionary* dict = [NSMutableDictionary dictionary];
[dict setValue:[EzyNativeStrings newNSString:config->getClientName().c_str()] forKey:@"clientName"];
[dict setValue:[EzyNativeStrings newNSString:config->getZoneName().c_str()] forKey:@"zoneName"];
NSDictionary* reconnectDict = [NSMutableDictionary dictionary];

EzyPingConfig* pingConfig = config->getPing();
NSDictionary* pingDict = [NSMutableDictionary dictionary];
[pingDict setValue:[NSNumber numberWithInt:pingConfig->getPingPeriod()] forKey:@"pingPeriod"];
[pingDict setValue:[NSNumber numberWithInt:pingConfig->getMaxLostPingCount()] forKey:@"maxLostPingCount"];
[dict setValue:pingDict forKey:@"ping"];

EzyReconnectConfig* reconnectConfig = config->getReconnect();
NSDictionary* reconnectDict = [NSMutableDictionary dictionary];
[reconnectDict setValue:[NSNumber numberWithInt:reconnectConfig->getMaxReconnectCount()] forKey:@"maxReconnectCount"];
[reconnectDict setValue:[NSNumber numberWithInt:reconnectConfig->getReconnectPeriod()] forKey:@"reconnectPeriod"];
[reconnectDict setValue:[NSNumber numberWithInt:reconnectConfig->isEnable()] forKey:@"enable"];
[dict setValue:reconnectDict forKey:@"reconnect"];

return dict;
}
@end
4 changes: 2 additions & 2 deletions ios/EzyClient/serializer/EzyEventSerializer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ -(NSDictionary*)serializeLostPingEvent: (EzyEvent*)event {
EzyLostPingEvent* mevent = (EzyLostPingEvent*)event;
NSDictionary* dict = [NSMutableDictionary dictionary];
int count = mevent->getCount();
[dict setValue:[NSNumber numberWithInt:count] forKey:@"reason"];
[dict setValue:[NSNumber numberWithInt:count] forKey:@"count"];
return dict;
}

-(NSDictionary*)serializeTryConnectEvent: (EzyEvent*)event {
EzyTryConnectEvent* mevent = (EzyTryConnectEvent*)event;
NSDictionary* dict = [NSMutableDictionary dictionary];
int count = mevent->getCount();
[dict setValue:[NSNumber numberWithInt:count] forKey:@"reason"];
[dict setValue:[NSNumber numberWithInt:count] forKey:@"count"];
return dict;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,11 @@
58B511F01A9E6C8500147676 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"EZY_DEBUG=1",
"$(inherited)",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
Expand Down
11 changes: 11 additions & 0 deletions lib/commonjs/ezy-config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/ezy-config.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion lib/commonjs/ezy-constants.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e2799a4

Please sign in to comment.