Skip to content

Commit

Permalink
实验一
Browse files Browse the repository at this point in the history
  • Loading branch information
haishui126 committed Jun 14, 2020
1 parent 6eb8d14 commit 5682062
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 256 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/out/
/.idea/
/common/target/
/chatroom-server/target/
/chatroom-client/target/
/db/
13 changes: 10 additions & 3 deletions chatroom-client/src/main/java/client/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client;

import client.dao.MessageDao;
import client.util.DB;
import common.DB;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
Expand All @@ -12,6 +11,7 @@

public class App extends Application {
private static Stage stage;
private static DB db;

@Override
public void start(Stage stage) throws Exception {
Expand All @@ -34,7 +34,14 @@ private static Parent loadFXML(String fxml) throws IOException {
}

public static void main(String[] args) {
DB.getInstance().getDao(MessageDao.class).createTable();
launch(args);
}

public static void initDB(String name) {
db = new DB(name);
}

public static DB getDB() {
return App.db;
}
}
51 changes: 31 additions & 20 deletions chatroom-client/src/main/java/client/ChatStage.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package client;

import client.dao.MessageDao;
import client.util.DB;
import common.Message;
import common.dao.MessageDao;
import common.model.Message;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
Expand All @@ -19,23 +19,24 @@
public class ChatStage extends Application {
private static final List<ChatStage> stageManager = new LinkedList<>();
private final String me;
private final String name;
private final String friend;
private VBox vBox;
private MessageDao messageDao;
ClientHandler handler = ClientHandler.getInstance();
private ListView<Message> messageListView;
private TextArea msgArea;

public ChatStage(String me, String name) {
public ChatStage(String me, String friend) {
this.me = me;
this.name = name;
this.friend = friend;
}

@Override
public void start(Stage stage) throws Exception {
stageManager.add(this);
layoutChildren();
stage.setScene(new Scene(vBox, 600, 500));
stage.setTitle(name);
stage.setTitle(friend);
stage.setOnCloseRequest(windowEvent -> stageManager.remove(this));
stage.show();
}
Expand All @@ -44,11 +45,11 @@ private void layoutChildren() {
vBox = new VBox();
vBox.setPadding(new Insets(20.0));
vBox.setSpacing(20.0);
messageDao = DB.getInstance().getDao(MessageDao.class);
messageDao = App.getDB().getDao(MessageDao.class);
messageListView = new ListView<>();
messageListView.setFocusTraversable(false);
messageListView.getItems().addAll(messageDao.getAll());
TextArea msgArea = new TextArea();
messageListView.getItems().addAll(messageDao.getAll(me, friend));
msgArea = new TextArea();
Button sendBtn = new Button("发送(回车)");
vBox.getChildren().addAll(messageListView, msgArea, sendBtn);
messageListView.setCellFactory(listView -> new ListCell<>() {
Expand All @@ -62,7 +63,7 @@ protected void updateItem(Message message, boolean empty) {
box.setAlignment(Pos.CENTER_RIGHT);
name.setText("我说:");
} else {
name.setText(ChatStage.this.name + "说:");
name.setText(ChatStage.this.friend + "说:");
}
name.setFont(Font.font(11));
name.setTextFill(Paint.valueOf("#999999"));
Expand All @@ -78,21 +79,31 @@ protected void updateItem(Message message, boolean empty) {
}
});

sendBtn.setOnMouseClicked(mouseEvent -> {
String msg = msgArea.getText();
if (!msg.isEmpty()) {
Message message = new Message(me, name, msg);
messageDao.save(message);
handler.sendMessage(message);
messageListView.getItems().add(message);
msgArea.setText("");
msgArea.setOnKeyPressed(keyEvent -> {
if (keyEvent.getCode() == KeyCode.ENTER) {
sendMessage();
}
});
sendBtn.setOnMouseClicked(mouseEvent -> {
sendMessage();
});
}

private void sendMessage() {
String msg = msgArea.getText();
if (!msg.isEmpty()) {
Message message = new Message(me, friend, msg);
messageDao.save(message);
handler.sendMessage(message);
messageListView.getItems().add(message);
messageListView.scrollTo(message);
msgArea.setText("");
}
}

public static ChatStage get(String friend) {
for (ChatStage chatStage : stageManager) {
if (chatStage.name.equals(friend)) {
if (chatStage.friend.equals(friend)) {
return chatStage;
}
}
Expand Down
28 changes: 7 additions & 21 deletions chatroom-client/src/main/java/client/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client;

import common.LoginResponse;
import common.Message;
import common.model.Response;
import common.model.Message;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
Expand All @@ -27,32 +27,18 @@ public static void resetInstance() {
handler = null;
}

// /**
// * 向服务端发送数据
// */
//连接时
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
channel = ctx.channel();
System.out.println("客户端与服务端通道-开启:" + ctx.channel().localAddress() + "channelActive");
}

// /**
// * channelInactive
// * <p>
// * channel 通道 Inactive 不活跃的
// * <p>
// * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
// */
// public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// System.out.println("客户端与服务端通道-关闭:" + ctx.channel().localAddress() + "channelInactive");
// }


//读取信息
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(msg);
if (msg instanceof LoginResponse) {
Platform.runLater(() -> loginResponseListener.onReceive((LoginResponse) msg));
if (msg instanceof Response) {
Platform.runLater(() -> loginResponseListener.onReceive((Response) msg));
} else if (msg instanceof Message) {
Platform.runLater(() -> messageListener.onReceive((Message) msg));
} else {
Expand All @@ -76,7 +62,7 @@ public void setMessageListener(MessageListener messageListener) {
}

public interface LoginResponseListener {
void onReceive(LoginResponse response);
void onReceive(Response response);
}

public interface MessageListener {
Expand Down
72 changes: 0 additions & 72 deletions chatroom-client/src/main/java/client/EchoClient.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import client.App;
import client.ClientHandler;
import client.util.AlertUtil;
import common.User;
import common.model.User;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
Expand Down Expand Up @@ -45,7 +45,6 @@ public void onConnect() {
}

EventLoopGroup group = new NioEventLoopGroup();
//关闭窗口后断开连接
ClientHandler clientHandler = ClientHandler.getInstance();
try {
Bootstrap b = new Bootstrap();
Expand All @@ -57,14 +56,16 @@ public void onConnect() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("正在连接中...");
ch.pipeline().addLast(new ObjectDecoder(1024 * 1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectDecoder(1024 * 1024,
ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
ch.pipeline().addLast(clientHandler);
ch.pipeline().addLast(new ObjectEncoder());
}
});
ChannelFuture cf = b.connect().sync(); // 异步连接服务器
cf.channel().writeAndFlush(new User(username));
System.out.println("服务端连接成功..."); // 连接完成
//关闭窗口后关闭socket
Platform.runLater(() -> btn.getScene().getWindow().setOnCloseRequest(windowEvent -> {
cf.channel().close();
group.shutdownGracefully();
Expand All @@ -73,6 +74,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
MainController.setUsername(username);
if (response.isSuccess()) {
try {
App.initDB(username);
App.setRoot("main");
} catch (IOException e) {
e.printStackTrace();
Expand Down
36 changes: 0 additions & 36 deletions chatroom-client/src/main/java/client/util/DB.java

This file was deleted.

19 changes: 0 additions & 19 deletions chatroom-client/src/main/resources/client/chat.fxml

This file was deleted.

Loading

0 comments on commit 5682062

Please sign in to comment.