Advertisement
JavNac

Client

Apr 26th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package com.ajnin.game;
  2.  
  3. import com.ajnin.game.serverobjects.TypeMessage;
  4. import com.jme3.app.SimpleApplication;
  5. import com.jme3.network.Client;
  6. import com.jme3.network.ClientStateListener;
  7. import com.jme3.network.Message;
  8. import com.jme3.network.MessageListener;
  9. import com.jme3.network.Network;
  10. import com.jme3.network.serializing.Serializer;
  11. import com.jme3.system.JmeContext;
  12. import java.io.IOException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author Ajnin
  19.  */
  20. public class MainClient extends SimpleApplication implements MessageListener<Client>, ClientStateListener {
  21.  
  22.     public Client myClient;
  23.  
  24.     public static void main(String[] args) {
  25.         MainClient app = new MainClient();
  26.         app.start(JmeContext.Type.Display); // standard display type
  27.     }
  28.  
  29.     @Override
  30.     public void simpleInitApp() {
  31.         Serializer.registerClass(TypeMessage.class);
  32.         connect("localhost", 7777);
  33.     }
  34.  
  35.     public void connect(String ip, int port) {
  36.         try {
  37.             System.out.println("Connecting...");
  38.             myClient = Network.connectToServer(ip, port);
  39.             myClient.addClientStateListener(this);
  40.             System.out.println(myClient.getGameName() + " " + myClient.getVersion());
  41.             myClient.start();
  42.             System.out.println("Connected");
  43.  
  44.             System.out.println("Sending Version and name info...");
  45.             String clientInformation = myClient.getGameName() + "|" + myClient.getVersion();
  46.             System.out.println("Collected information: " + clientInformation + " now sending...");
  47.             Message gameInformation = new TypeMessage((String) clientInformation, "Game Identity");
  48.             gameInformation.setReliable(true);
  49.             myClient.send(gameInformation);
  50.             System.out.println("Version and name sent.");
  51.  
  52.         } catch (IOException ex) {
  53.             Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
  54.         }
  55.     }
  56.  
  57.     @Override
  58.     public void simpleUpdate(float tpf) {
  59.     }
  60.  
  61.     @Override
  62.     public void messageReceived(Client source, Message message) {
  63.         if (message instanceof TypeMessage) {
  64.             TypeMessage information = (TypeMessage) message;
  65.             if (information.getType().equals("Broadcast")) {
  66.                 System.out.println(information.getMessage());
  67.             }
  68.         }
  69.     }
  70.  
  71.     @Override
  72.     public void destroy() {
  73.         myClient.close();
  74.         super.destroy();
  75.     }
  76.  
  77.     @Override
  78.     public void clientConnected(Client c) {
  79.     }
  80.  
  81.     @Override
  82.     public void clientDisconnected(Client c, DisconnectInfo info) {
  83.         System.out.println("Kicked: " + info);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement