我开始玩游戏来学习制作在线游戏的基础知识。虽然,它并没有像我希望的那样好。从它看起来它的客户端由于某种原因没有收到数据包。如标题所述,我使用的是Kryonet和Slick2D。
发生的事情是我的玩家永远不会被吸引,因为玩家列表没有得到更新。
我一直在寻找两者的文档和无数的例子。我似乎无法找到我的错误。
服务器代码:
package main;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import com.esotericsoftware.kryonet.Server;
import lookup.Settings;
import networking.NListener;
import networking.Network;
import structs.Player;
public class ServerMain extends BasicGame{
/*
* Server = Our public server object.
* Players = A list containing all players coords and connection ID.
*/
public static Server server;
public static List<Player>players = new ArrayList<Player>();
/* Our Constructor to set up our server and game. */
public ServerMain() throws IOException{
super(Settings.Title);
/*We register our kryos and our server.*/
server = new Server();
Network.register(server);
/*We init our listener*/
server.addListener(new NListener());
/*Binding and starting the server*/
server.bind(Settings.tcport, Settings.udport);
server.start();
}
public void render(GameContainer arg0, Graphics g) throws SlickException {
//Test for now to see if players actually do connect.
g.drawString("Players connected:"+Integer.toString(players.size()), 10, 100);
}
public void init(GameContainer arg0) throws SlickException {}
public void update(GameContainer arg0, int arg1) throws SlickException {}
public static void main(String[] args) throws IOException{
//Basic slick2d stuff we set our display non fullscreen and our title from our settings.
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new ServerMain());
appgc.setDisplayMode(400, 400, false);
appgc.start();
}catch(SlickException e){
System.out.println("error");
}
}
}
服务器NListener:
package networking;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import lookup.Settings;
import main.ServerMain;
import networking.Network.*;
import structs.Player;
public class NListener extends Listener{
@Override
/*Connected is called when a client is connected*/
public void connected (Connection c) {
//We make a new player instance.
Player newPly = new Player();
//Setting it to the starting position cuz we have no saving so far :)
newPly.x = Settings.startX;
newPly.y = Settings.startY;
newPly.c = c;
//We add our newly made player to the list.
ServerMain.players.add(newPly);
//make an instance of an updateList packet
UpdateList outpck = new UpdateList();
//Putting our list into the newly made packet
outpck.players = ServerMain.players;
//Send said paceket to all clients.
ServerMain.server.sendToAllTCP(outpck);
}
@Override
public void disconnected (Connection c) {
//TODO: find where connection is in list and then delete that
}
@Override
public void received(Connection c, Object o){
//TODO: act upon a moving client.
}
}
客户:
package main;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
import com.esotericsoftware.kryonet.Client;
import lookup.Settings;
import networking.NListener;
import networking.Network;
import structs.Player;
public class BaseGame extends BasicGame{
/*
* CurrMap = is the current map to display
* players = a list of players to draw.
* client = our client object.
* drawStr = a test string I draw to test my code (:
* */
public TiledMap currmap;
public static List<Player>players = new ArrayList<Player>();
public Client client;
public String drawStr = "nothing yet";
/*Our constructor to init both game and client*/
public BaseGame(String title) {
super(title);
//initing our client
client = new Client();
client.start();
//Registering our classes and adding our listener
Network.register(client);
client.addListener(new NListener());
//Lets try connect :)
try{
client.connect(Settings.timeout, Settings.IP, Settings.tcport, Settings.udport);
} catch(IOException e){
e.printStackTrace();
}
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
//Render our map. at 0,0
currmap.render(0, 0);
//Lets go through all players and draw them.
for (int i = 0; i < players.size(); i++) {
Player Tref = players.get(i);
g.fillRect(Tref.x*Settings.tileSize, Tref.y*Settings.tileSize, Settings.tileSize, Settings.tileSize);
}
//this is more for testing to see if player got updated or not.
g.drawString(Integer.toString(players.size()), 10, 100);
g.drawString(drawStr, 10, 120);
g.drawString(players.toString(), 10, 350);
}
//Initing our tiled map
public void init(GameContainer arg0) throws SlickException {
currmap = new TiledMap("res/maptest.tmx");
}
public void update(GameContainer arg0, int arg1) throws SlickException {
//TODO: Update players.
}
public static void main(String[] args){
//Basic slick2d stuff we set our display non fullscreen and our title from our settings.
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new BaseGame(Settings.Title));
appgc.setDisplayMode(400, 400, false);
appgc.start();
}catch(SlickException e){
System.out.println("error");
}
}
}
客户端NListener:
package networking;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import main.BaseGame;
import networking.Network.UpdateList;
public class NListener extends Listener{
/*
* This is our network listener.
* Here we handle our receive functions and cleaning up after ourselves :)
*/
@Override
public void connected(Connection c){
}
@Override
public void received (Connection connection, Object object) {
if (object instanceof Network.UpdateList) {
UpdateList inpck = new UpdateList();
inpck = (UpdateList)object;
BaseGame.players = inpck.players;
}
}
}
普通网络课程:
package networking;
import java.util.ArrayList;
import java.util.List;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.EndPoint;
import structs.Player;
public class Network {
static public void register(EndPoint Ep){
//Getting the client kryo
Kryo kryo = Ep.getKryo();
//Register our classes
kryo.register(UpdateList.class);
}
/*
* Updatelist is for updating list at client.
*
* */
static public class UpdateList {public List<Player>players = new ArrayList<Player>();}
}
答案 0 :(得分:0)
我试图重现你的问题,但它对我有用。 你可以尝试这个代码,除了一些东西,它基本上是你的: 如果收到包,服务器控制台窗口将打印出“已连接”。 客户端控制台窗口将打印出“test”。 如果这对您不起作用,我认为您的机器/网络设置或其他项目的某些部分负责为您搞砸。
package stack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
import com.esotericsoftware.kryonet.Client;
public class BaseGame extends BasicGame{
public TiledMap currmap;
public static List<Player>players = new ArrayList<Player>();
public Client client;
public String drawStr = "nothing yet";
/*Our constructor to init both game and client*/
public BaseGame(String title) {
super(title);
//initing our client
client = new Client();
client.start();
//Registering our classes and adding our listener
Network.register(client);
client.addListener(new NListener2());
//Lets try connect :)
try{
client.connect(5000, "localhost", 55555, 55556);
} catch(IOException e){
e.printStackTrace();
}
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
//Render our map. at 0,0
//Lets go through all players and draw them.
for (int i = 0; i < players.size(); i++) {
Player Tref = players.get(i);
}
//this is more for testing to see if player got updated or not.
g.drawString(Integer.toString(players.size()), 10, 100);
g.drawString(drawStr, 10, 120);
g.drawString(players.toString(), 10, 350);
}
//Initing our tiled map
public void init(GameContainer arg0) throws SlickException {
}
public void update(GameContainer arg0, int arg1) throws SlickException {
//TODO: Update players.
}
public static void main(String[] args){
//Basic slick2d stuff we set our display non fullscreen and our title from our settings.
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new BaseGame("yo23"));
appgc.setDisplayMode(400, 400, false);
appgc.start();
}catch(SlickException e){
System.out.println("error");
}
}
}
-
package stack;
import java.util.ArrayList;
import java.util.List;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.EndPoint;
public class Network {
static public void register(EndPoint Ep){
//Getting the client kryo
Kryo kryo = Ep.getKryo();
//Register our classes
kryo.register(String.class);
}
/*
* Updatelist is for updating list at client.
*
* */
static public class UpdateList {public List<Player>players = new ArrayList<Player>();}
}
-
package stack;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
public class NListener extends Listener{
@Override
/*Connected is called when a client is connected*/
public void connected (Connection c) {
//We make a new player instance.
Player newPly = new Player();
//Setting it to the starting position cuz we have no saving so far :)
//We add our newly made player to the list.
ServerMain.players.add(newPly);
System.out.println("connected");
//make an instance of an updateList packet
//Putting our list into the newly made packet
//Send said paceket to all clients.
ServerMain.server.sendToAllTCP("test");
}
@Override
public void disconnected (Connection c) {
//TODO: find where connection is in list and then delete that
}
@Override
public void received(Connection c, Object o){
//TODO: act upon a moving client.
}
}
-
package stack;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
public class NListener2 extends Listener{
@Override
public void connected(Connection c){
}
@Override
public void received (Connection connection, Object object) {
if (object instanceof String) {
String temp = (String)object;
System.out.println(temp);
}
}
}
-
package stack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import com.esotericsoftware.kryonet.Server;
public class ServerMain extends BasicGame{
/*
* Server = Our public server object.
* Players = A list containing all players coords and connection ID.
*/
public static Server server;
public static List<Player>players = new ArrayList<Player>();
/* Our Constructor to set up our server and game. */
public ServerMain() throws IOException{
super("yo");
/*We register our kryos and our server.*/
server = new Server();
Network.register(server);
/*We init our listener*/
server.addListener(new NListener());
/*Binding and starting the server*/
server.bind(55555, 55556);
server.start();
}
public void render(GameContainer arg0, Graphics g) throws SlickException {
//Test for now to see if players actually do connect.
g.drawString("Players connected:"+Integer.toString(players.size()), 10, 100);
}
public void init(GameContainer arg0) throws SlickException {}
public void update(GameContainer arg0, int arg1) throws SlickException {}
public static void main(String[] args) throws IOException{
//Basic slick2d stuff we set our display non fullscreen and our title from our settings.
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new ServerMain());
appgc.setDisplayMode(400, 400, false);
appgc.start();
}catch(SlickException e){
System.out.println("error");
}
}
}