Package edu.ups.gamedev.net.tests

Source Code of edu.ups.gamedev.net.tests.ChatClient

package edu.ups.gamedev.net.tests;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNClient;
import com.captiveimagination.jgn.event.DebugListener;
import com.captiveimagination.jgn.event.DynamicMessageAdapter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
* @author Matthew D. Hicks
*/
public class ChatClient extends DynamicMessageAdapter implements ActionListener {
  private JGNClient client;
  private String nickname;
  private JTextPane textPane;
  private JTextField textField;

  public ChatClient() throws Exception {
    JGN.register(NamedChatMessage.class);

    InetSocketAddress reliableAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    InetSocketAddress fastAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    client = new JGNClient(reliableAddress, fastAddress);
    client.addMessageListener(this);
    client.addMessageListener(new DebugListener("ChatClient>"));
    JGN.createThread(client).start();

    InetSocketAddress reliableServerAddress = new InetSocketAddress(InetAddress.getByName("10.100.1.49"), 1100);
    InetSocketAddress fastServerAddress = new InetSocketAddress(InetAddress.getByName("10.100.1.49"), 2000);

    client.connectAndWait(reliableServerAddress, fastServerAddress, 5000);
    nickname = JOptionPane.showInputDialog("Connection established to server\n\nPlease enter the name you wish to use?");
    initGUI();
  }

  private void initGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)// TODO fix
    frame.setTitle("Chat Client - " + nickname);
    frame.setSize(300, 300);
    Container c = frame.getContentPane();
    c.setLayout(new BorderLayout());
    textPane = new JTextPane();
    textPane.setText("");
    textPane.setEditable(false);
    c.add(BorderLayout.CENTER, textPane);
    textField = new JTextField();
    textField.addActionListener(this);
    c.add(BorderLayout.SOUTH, textField);
    frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent evt) {
    String txt = textField.getText().trim();
    if (txt.length() > 0) {
      NamedChatMessage message = new NamedChatMessage();
      message.setPlayerName(nickname);
      message.setText(txt);

      if (txt.startsWith("/")) {
        client.sendToServer(message);
      } else {
        client.broadcast(message);
      }

      textField.setText("");
      writeMessage(client.getPlayerId(), nickname, message.getText());
    }
  }

  public void messageReceived(NamedChatMessage message) {
    writeMessage(message.getPlayerId(), message.getPlayerName(), message.getText());
  }

  private void writeMessage(short playerId, String playerName, String text) {
    String message = "[" + playerName + ":" + playerId + "]: " + text;
    if (textPane.getText().length() == 0) {
      textPane.setText(message);
    } else {
      textPane.setText(textPane.getText() + "\r\n" + message);
    }
    System.out.println(message);
  }

  public static void main(String[] args) throws Exception {
    new ChatClient();
  }
}
TOP

Related Classes of edu.ups.gamedev.net.tests.ChatClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.