1

所以我有一个内置于 libGDX 的多人游戏,在 onConnectDone 的桌面上运行得非常好(获取 WarpResponseResultCode.SUCCESS)。在android上,结果代码是CONNECTION_ERROR,我不知道这是否与使用错误的App42MultiPlayerGaming.jar有关(我在android和桌面上使用相同的)或者是否还有其他东西)

WarpControler.java处理回调

package appwarp;


import java.util.HashMap;

import com.greenpipestudios.paradox.gamestate.PlayState;
import com.shephertz.app42.gaming.multiplayer.client.WarpClient;
import com.shephertz.app42.gaming.multiplayer.client.command.WarpResponseResultCode;
import com.shephertz.app42.gaming.multiplayer.client.events.RoomEvent;

public class WarpController {

private static WarpController instance;





private boolean showLog = true;

private final String apiKey = "SHHHHHH!!!";
private final String secretKey = "SHHHHHH!!!";

private WarpClient warpClient;

public String localUser;
private String roomId;
public int numUsers;

private boolean isConnected = false;
boolean isUDPEnabled = false;

private WarpListener warpListener ;
public static final int minUsers = 1;
public static final int maxUsers = 3;

private int STATE;
// Game state constants
public static final int WAITING = 1;
public static final int STARTED = 2;
public static final int COMPLETED = 3;
public static final int FINISHED = 4;

// Game completed constants
public static final int GAME_WIN = 5;
public static final int GAME_LOOSE = 6;

//


public WarpController() {
    initAppwarp();
    warpClient.addConnectionRequestListener(new ConnectionListener(this));
    warpClient.addChatRequestListener(new ChatListener(this));
    warpClient.addZoneRequestListener(new ZoneListener(this));
    warpClient.addRoomRequestListener(new RoomListener(this));
    warpClient.addNotificationListener(new NotificationListener(this));
    numUsers = 0;
}


public static WarpController getInstance(){
    if(instance == null){
        instance = new WarpController();
    }
    return instance;
}

public void startApp(String localUser){
    this.localUser = localUser;
    warpClient.connectWithUserName(localUser);

}

public void setListener(WarpListener listener){
    this.warpListener = listener;
}

public void stopApp(){
    if(isConnected){
        warpClient.unsubscribeRoom(roomId);
        warpClient.leaveRoom(roomId);
    }
    warpClient.disconnect();
}

private void initAppwarp(){
    try {
        WarpClient.initialize(apiKey, secretKey);
        WarpClient.enableTrace(true);
        warpClient = WarpClient.getInstance();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void sendGameUpdate(String msg){
    if(isConnected){
        if(isUDPEnabled){
            warpClient.sendUDPUpdatePeers((localUser+"#@"+msg).getBytes());
        }else{
            warpClient.sendUpdatePeers((localUser+"#@"+msg).getBytes());
        }
    }
}

public void updateResult(int code, String msg){
    if(isConnected){
        STATE = COMPLETED;
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("result", code);
        warpClient.lockProperties(properties);
    }
}

public void onConnectDone(boolean status){
    log("onConnectDone: "+status);
    if(status){
        warpClient.initUDP();
        warpClient.joinRoomInRange(1, maxUsers - 1, false);
    }else{
        isConnected = false;
        System.out.println("onConnectDone error");
        handleError();
    }
}

public void onDisconnectDone(boolean status){

}

public void onRoomCreated(String roomId){
    if(roomId!=null){
        warpClient.joinRoom(roomId);
    }else{
        System.out.println("onRoomCreated error");
        handleError();
    }
}

public void onJoinRoomDone(RoomEvent event){
    log("onJoinRoomDone: "+event.getResult());
    if(event.getResult()==WarpResponseResultCode.SUCCESS){// success case
        this.roomId = event.getData().getId();
        warpClient.subscribeRoom(roomId);
    }else if(event.getResult()==WarpResponseResultCode.RESOURCE_NOT_FOUND){// no such room found
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("result", "");
        warpClient.createRoom("superjumper", "shephertz", maxUsers, data);
    }else{
        warpClient.disconnect();
        System.out.println("onJoinRoomDone error");
        handleError();
    }
}

public void onRoomSubscribed(String roomId){
    log("onSubscribeRoomDone: "+roomId);
    if(roomId!=null){
        isConnected = true;
        warpClient.getLiveRoomInfo(roomId);
    }else{
        warpClient.disconnect();
        System.out.println("onRoomSubscribed error");
        handleError();
    }
}

public void onGetLiveRoomInfo(String[] liveUsers){
    log("onGetLiveRoomInfo: "+liveUsers.length);
    numUsers = liveUsers.length;
    PlayState ps  = (PlayState) warpListener;
    ps.lastonein = true;
    if(numUsers >=minUsers ){
        startGame();    
    }else{
        waitForOtherUser();
    }
    warpListener.setPlayerColor(numUsers);
}

public void onUserJoinedRoom(String roomId, String userName){
    /*
     * if room id is same and username is different then start the game
     */

    numUsers++;
    if(!localUser.equals(userName) && numUsers >= minUsers){
        startGame();
    }
    warpListener.onUserJoined(roomId, userName);
}

public void onSendChatDone(boolean status){
    log("onSendChatDone: "+status);
}

public void onGameUpdateReceived(String message){


//      log("onMoveUpdateReceived: message"+ message );
    String userName = message.substring(0, message.indexOf("#@"));
    //String data = message.substring(message.indexOf("#@")+2, message.length());
    if(!localUser.equals(userName) && message != null){
        warpListener.onGameUpdateReceived(message);
    }
}

public void onResultUpdateReceived(String userName, int code){
    if(localUser.equals(userName)==false){
        STATE = FINISHED;
        warpListener.onGameFinished(code, true);
    }else{
        warpListener.onGameFinished(code, false);
    }
}

public void onUserLeftRoom(String roomId, String userName){
    log("onUserLeftRoom "+userName+" in room "+roomId);
    warpListener.onUserLeft(roomId, userName);
    numUsers--;
}

public int getState(){
    return this.STATE;
}

private void log(String message){
    if(showLog){
        System.out.println(message);
    }
}

private void startGame(){
    STATE = STARTED;
    warpListener.onGameStarted("Start the Game");
}

private void waitForOtherUser(){
    STATE = WAITING;
    warpListener.onWaitingStarted("Waiting for other user");
}

private void handleError(){
    if(roomId!=null && roomId.length()>0){
        warpClient.deleteRoom(roomId);
    }
    System.out.println("Kicked for error! Contact the developer or the U.S. President!");
    disconnect();
}

public void handleLeave(){
    if(isConnected){
        warpClient.unsubscribeRoom(roomId);
        warpClient.leaveRoom(roomId);
        if(STATE!=STARTED){
            warpClient.deleteRoom(roomId);
        }
        warpClient.disconnect();
    }
}

private void disconnect(){
    warpClient.removeConnectionRequestListener(new ConnectionListener(this));
    warpClient.removeChatRequestListener(new ChatListener(this));
    warpClient.removeZoneRequestListener(new ZoneListener(this));
    warpClient.removeRoomRequestListener(new RoomListener(this));
    warpClient.removeNotificationListener(new NotificationListener(this));
    warpClient.disconnect();
}
}

Connection Listener.java调用回调

package appwarp;

import com.shephertz.app42.gaming.multiplayer.client.command.WarpResponseResultCode;
import com.shephertz.app42.gaming.multiplayer.client.events.ConnectEvent;
import com.shephertz.app42.gaming.multiplayer.client.listener.ConnectionRequestListener;

public class ConnectionListener implements ConnectionRequestListener {

WarpController callBack;

public ConnectionListener(WarpController callBack){
    this.callBack = callBack;
}

public void onConnectDone(ConnectEvent e) {
    System.out.println("Connect Code: " + e.getResult());
    if(e.getResult()==WarpResponseResultCode.SUCCESS){

        callBack.onConnectDone(true);
    }else{
        callBack.onConnectDone(false);
    }
}

public void onDisconnectDone(ConnectEvent e) {

}

@Override
public void onInitUDPDone (byte result) {
    if(result==WarpResponseResultCode.SUCCESS){
        callBack.isUDPEnabled = true;
    }
}

}
4

2 回答 2

0

有点尴尬,但我会把这个留给需要解决这个问题的其他人:我所要做的就是在 android manifest xml 中启用互联网权限。

于 2015-05-17T00:48:43.483 回答
0

是的,您必须在 AndroidManifest.xml 文件中提供互联网权限。

于 2015-05-17T06:36:17.073 回答