Skip to content

Commit 718bd5a

Browse files
author
magiclu550
committed
create client
1 parent 3a0caf8 commit 718bd5a

File tree

10 files changed

+418
-0
lines changed

10 files changed

+418
-0
lines changed

.idea/compiler.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

panel-client.iml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/resources" type="java-resource" />
9+
<excludeFolder url="file://$MODULE_DIR$/target" />
10+
</content>
11+
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="Maven: io.netty:netty-all:5.0.0.Alpha1" level="project" />
14+
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.47" level="project" />
15+
</component>
16+
</module>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cn.jsmod2.client.panel;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.EventLoopGroup;
6+
import io.netty.channel.nio.NioEventLoopGroup;
7+
import io.netty.channel.socket.nio.NioSocketChannel;
8+
9+
import java.util.concurrent.CountDownLatch;
10+
11+
public class Client {
12+
private static class SingletonHolder {
13+
static final Client instance = new Client();
14+
}
15+
public static Client getInstance(){
16+
return SingletonHolder.instance;
17+
}
18+
private EventLoopGroup group;
19+
private Bootstrap b;
20+
private ChannelFuture cf ;
21+
private ClientInitializer clientInitializer;
22+
private CountDownLatch lathc;
23+
private Client(){
24+
lathc = new CountDownLatch(0);
25+
clientInitializer = new ClientInitializer(lathc);
26+
group = new NioEventLoopGroup();
27+
b = new Bootstrap();
28+
b.group(group)
29+
.channel(NioSocketChannel.class)
30+
.handler(clientInitializer);
31+
}
32+
public void connect(){
33+
//192.168.43.51测试端口8766 192.168.43.102 线上端口8765
34+
try {
35+
this.cf = b.connect("127.0.01", 8888).sync();
36+
System.out.println("远程服务器已经连接, 可以进行数据交换..");
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
} finally {
40+
}
41+
}
42+
public ChannelFuture getChannelFuture(){
43+
if(this.cf == null) {
44+
this.connect();
45+
}
46+
if(!this.cf.channel().isActive()){
47+
this.connect();
48+
}
49+
return this.cf;
50+
}
51+
public void close(){
52+
try {
53+
this.cf.channel().closeFuture().sync();
54+
this.group.shutdownGracefully();
55+
} catch (InterruptedException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
public String setMessage(String msg) {
60+
try {
61+
ChannelFuture cf = getInstance().getChannelFuture();//单例模式获取ChannelFuture对象
62+
cf.channel().writeAndFlush(msg);
63+
//发送数据控制门闩加一
64+
lathc = new CountDownLatch(1);
65+
clientInitializer.resetLathc(lathc);
66+
lathc.await();//开始等待结果返回后执行下面的代码
67+
return clientInitializer.getServerResult();
68+
}catch (InterruptedException e){
69+
e.printStackTrace();
70+
}
71+
return null;
72+
}
73+
74+
75+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.jsmod2.client.panel;
2+
3+
import io.netty.channel.ChannelHandlerAdapter;
4+
import io.netty.channel.ChannelHandlerContext;
5+
6+
import java.util.concurrent.CountDownLatch;
7+
8+
public class ClientHandler extends ChannelHandlerAdapter {
9+
10+
private CountDownLatch lathc;
11+
private String result;
12+
public ClientHandler(CountDownLatch lathc) {
13+
this.lathc = lathc;
14+
}
15+
16+
@Override
17+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
18+
19+
}
20+
21+
@Override
22+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
23+
result = (String) msg;
24+
lathc.countDown();// 消息接收后释放同步锁,lathc是从Client加一传回来的
25+
}
26+
27+
@Override
28+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
29+
30+
}
31+
32+
@Override
33+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
34+
ctx.close();
35+
}
36+
public void resetLatch(CountDownLatch lathc) {
37+
this.lathc = lathc;
38+
}
39+
40+
public String getResult() {
41+
return result;
42+
}
43+
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.jsmod2.client.panel;
2+
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.socket.SocketChannel;
5+
import io.netty.handler.codec.string.StringDecoder;
6+
import io.netty.handler.codec.string.StringEncoder;
7+
import io.netty.handler.timeout.ReadTimeoutHandler;
8+
9+
import java.util.concurrent.CountDownLatch;
10+
11+
public class ClientInitializer extends ChannelInitializer<SocketChannel> {
12+
13+
private CountDownLatch lathc;
14+
public ClientInitializer(CountDownLatch lathc) {
15+
this.lathc = lathc;
16+
}
17+
private ClientHandler handler;
18+
@Override
19+
protected void initChannel(SocketChannel sc) throws Exception {
20+
handler = new ClientHandler(lathc);
21+
sc.pipeline().addLast(new StringDecoder());//进行字符串的编解码设置
22+
sc.pipeline().addLast(new StringEncoder());
23+
sc.pipeline().addLast(new ReadTimeoutHandler(60));//设置超时时间
24+
sc.pipeline().addLast(handler);
25+
}
26+
public String getServerResult(){
27+
return handler.getResult();
28+
}
29+
public void resetLathc(CountDownLatch lathc) {
30+
handler.resetLatch(lathc);
31+
}
32+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cn.jsmod2.client.panel;
2+
3+
import javafx.collections.ObservableList;
4+
import javafx.scene.control.TextArea;
5+
6+
import java.io.IOException;
7+
import java.io.OutputStream;
8+
9+
/**
10+
* @ClassName: ConsoleOutputStream
11+
* @Description: OutputStream
12+
* @Author: GNX-Susanoo
13+
* @Date: 2019/7/20 19:39
14+
* @Version: 1.0
15+
*/
16+
public class ConsoleOutputStream extends OutputStream {
17+
private TextArea textArea;
18+
private int lineHeight = 100;
19+
private volatile boolean scroll = true;
20+
21+
@Override
22+
public void write(int b) throws IOException {
23+
if (textArea == null) {
24+
return;
25+
}
26+
textArea.appendText(String.valueOf((char) b));
27+
cleanUpLin(textArea.getParagraphs());
28+
if (scroll) {
29+
textArea.setScrollTop(Double.MAX_VALUE);
30+
}
31+
}
32+
33+
@Override
34+
public void write(byte[] b) throws IOException {
35+
if (textArea == null) {
36+
return;
37+
}
38+
textArea.appendText(new String(b));
39+
cleanUpLin(textArea.getParagraphs());
40+
if (scroll) {
41+
textArea.setScrollTop(Double.MAX_VALUE);
42+
}
43+
}
44+
45+
public void write(String string) throws IOException {
46+
if (textArea == null) {
47+
return;
48+
}
49+
textArea.appendText(string);
50+
cleanUpLin(textArea.getParagraphs());
51+
if (scroll) {
52+
textArea.setScrollTop(Double.MAX_VALUE);
53+
}
54+
}
55+
56+
private void cleanUpLin(ObservableList<CharSequence> charSequencesList) {
57+
if (charSequencesList.size() > lineHeight + 1) {
58+
textArea.deleteText(0, charSequencesList.get(0).length() + 1);
59+
cleanUpLin(charSequencesList);
60+
}
61+
}
62+
63+
public int getLineHeight() {
64+
return lineHeight;
65+
}
66+
67+
public void setLineHeight(int lineHeight) {
68+
this.lineHeight = lineHeight;
69+
}
70+
71+
public boolean isScroll() {
72+
return scroll;
73+
}
74+
75+
public void setScroll(boolean scroll) {
76+
this.scroll = scroll;
77+
}
78+
79+
public void setTextArea(TextArea textArea) {
80+
this.textArea = textArea;
81+
}
82+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.jsmod2.client.panel;
2+
3+
import com.alibaba.fastjson.JSON;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class JsonRequester {
9+
10+
private Map<String,String> map = new HashMap<String, String>();
11+
12+
public JsonRequester append(String key,String value){
13+
map.put(key,value);
14+
return this;
15+
}
16+
17+
public String toJSON(){
18+
return JSON.toJSONString(map);
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.jsmod2.client.panel;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Parent;
6+
import javafx.scene.Scene;
7+
import javafx.stage.Stage;
8+
9+
public class Main extends Application {
10+
11+
public static void main(String[] args) {
12+
launch(args);
13+
}
14+
15+
@Override
16+
public void start(Stage primaryStage) throws Exception {
17+
Parent root = FXMLLoader.load(getClass().getResource("/ui/start.fxml"));
18+
primaryStage.setTitle("Jsmod2-Control-Panel");
19+
primaryStage.setResizable(false);
20+
primaryStage.setScene(new Scene(root, 650, 450));
21+
primaryStage.show();
22+
}
23+
24+
}

0 commit comments

Comments
 (0)