chatFrame.setNames(updateNames.names);
return;
}
if (object instanceof ChatMessage) {
ChatMessage chatMessage = (ChatMessage)object;
chatFrame.addMessage(chatMessage.text);
return;
}
}
public void disconnected (Connection connection) {
EventQueue.invokeLater(new Runnable() {
public void run () {
// Closing the frame calls the close listener which will stop the client's update thread.
chatFrame.dispose();
}
});
}
});
// Request the host from the user.
String input = (String)JOptionPane.showInputDialog(null, "Host:", "Connect to chat server", JOptionPane.QUESTION_MESSAGE,
null, null, "localhost");
if (input == null || input.trim().length() == 0) System.exit(1);
final String host = input.trim();
// Request the user's name.
input = (String)JOptionPane.showInputDialog(null, "Name:", "Connect to chat server", JOptionPane.QUESTION_MESSAGE, null,
null, "Test");
if (input == null || input.trim().length() == 0) System.exit(1);
name = input.trim();
// All the ugly Swing stuff is hidden in ChatFrame so it doesn't clutter the KryoNet example code.
chatFrame = new ChatFrame(host);
// This listener is called when the send button is clicked.
chatFrame.setSendListener(new Runnable() {
public void run () {
ChatMessage chatMessage = new ChatMessage();
chatMessage.text = chatFrame.getSendText();
client.sendTCP(chatMessage);
}
});
// This listener is called when the chat window is closed.