Skip to content

Commit

Permalink
@dev change guide
Browse files Browse the repository at this point in the history
  • Loading branch information
sean committed Sep 7, 2021
1 parent db187f9 commit b4184a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,53 @@ serverRemote.run();



Run Server in spring boot project

```java
public class SpringBootStartEvent implements ApplicationListener<SpringApplicationEvent>, ApplicationContextAware {
private ApplicationContext applicationContext;

@Bean(name = "serverRemote")
ServerRemote createNettyRemote() {
return ServerRemote.newServerInstance(8080, requestPlus -> {
//deal with request and return response
return ResponsePlus.build(0, "ok");
});
}

@Override
public void onApplicationEvent(SpringApplicationEvent springApplicationEvent) {
Object bean = applicationContext.getBean("serverRemote");
if (bean instanceof ServerRemote) {
try {
((ServerRemote) bean).run();
} catch (Exception e) {
e.printStackTrace();
}
log.info("Start netty server successfully");
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
```



Run a client with follow code

```java
ConnectManager connectManager = ConnectManager.newConnectInstance("127.0.0.1", 2222);
String remoteAddress = "127.0.0.1";
int port = 8080;
ConnectManager connectManager = ConnectManager.newConnectInstance(remoteAddress, port);
RequestPlus requestPlus = new RequestPlus();
String requestId = connectManager.sendMsg(requestPlus);
connectManager.handleResult(requestId, responsePlus -> {
//deal with response
//deal with response
});
});
```

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/tos/netty/client/ConnectManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class ConnectManagerTest {

@Test
public void sendMsg() throws ExecutionException, InterruptedException {
ConnectManager connectManager = ConnectManager.newConnectInstance("127.0.0.1", 8080);
String remoteAddress = "127.0.0.1";
int port = 8080;
ConnectManager connectManager = ConnectManager.newConnectInstance(remoteAddress, port);
RequestPlus requestPlus = new RequestPlus();
String requestId = connectManager.sendMsg(requestPlus);
connectManager.handleResult(requestId, responsePlus -> {
Expand Down

0 comments on commit b4184a0

Please sign in to comment.