Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ajnin.game;
- import com.ajnin.game.serverobjects.TypeMessage;
- import com.jme3.app.SimpleApplication;
- import com.jme3.network.Client;
- import com.jme3.network.ClientStateListener;
- import com.jme3.network.Message;
- import com.jme3.network.MessageListener;
- import com.jme3.network.Network;
- import com.jme3.network.serializing.Serializer;
- import com.jme3.system.JmeContext;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Ajnin
- */
- public class MainClient extends SimpleApplication implements MessageListener<Client>, ClientStateListener {
- public Client myClient;
- public static void main(String[] args) {
- MainClient app = new MainClient();
- app.start(JmeContext.Type.Display); // standard display type
- }
- @Override
- public void simpleInitApp() {
- Serializer.registerClass(TypeMessage.class);
- connect("localhost", 7777);
- }
- public void connect(String ip, int port) {
- try {
- System.out.println("Connecting...");
- myClient = Network.connectToServer(ip, port);
- myClient.addClientStateListener(this);
- System.out.println(myClient.getGameName() + " " + myClient.getVersion());
- myClient.start();
- System.out.println("Connected");
- System.out.println("Sending Version and name info...");
- String clientInformation = myClient.getGameName() + "|" + myClient.getVersion();
- System.out.println("Collected information: " + clientInformation + " now sending...");
- Message gameInformation = new TypeMessage((String) clientInformation, "Game Identity");
- gameInformation.setReliable(true);
- myClient.send(gameInformation);
- System.out.println("Version and name sent.");
- } catch (IOException ex) {
- Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @Override
- public void simpleUpdate(float tpf) {
- }
- @Override
- public void messageReceived(Client source, Message message) {
- if (message instanceof TypeMessage) {
- TypeMessage information = (TypeMessage) message;
- if (information.getType().equals("Broadcast")) {
- System.out.println(information.getMessage());
- }
- }
- }
- @Override
- public void destroy() {
- myClient.close();
- super.destroy();
- }
- @Override
- public void clientConnected(Client c) {
- }
- @Override
- public void clientDisconnected(Client c, DisconnectInfo info) {
- System.out.println("Kicked: " + info);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement