Java 如何在Socket游戏服务器上组织命令/处理程序?
我正在为我正在创建的MMORPG开发一个套接字服务器,我不确定如何组织命令和处理程序以提高可读性 我将从数据[0]获取命令,例如ban、kick、identify等。我已经了解到,您可以使用HashMap和接口命令,并为实现该命令的每个处理程序/命令创建一个新类。然后创建一个HashMap并从那里开始。我也读过,你可以反思。我想做一个有经验的程序员会做的事情 客户端类:Java 如何在Socket游戏服务器上组织命令/处理程序?,java,sockets,Java,Sockets,我正在为我正在创建的MMORPG开发一个套接字服务器,我不确定如何组织命令和处理程序以提高可读性 我将从数据[0]获取命令,例如ban、kick、identify等。我已经了解到,您可以使用HashMap和接口命令,并为实现该命令的每个处理程序/命令创建一个新类。然后创建一个HashMap并从那里开始。我也读过,你可以反思。我想做一个有经验的程序员会做的事情 客户端类: import java.io.BufferedReader; import java.io.IOException; impo
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client implements Runnable {
private Socket socket;
private Server server;
private boolean isConnected;
private BufferedReader in;
private PrintWriter out;
public Client(Socket socket, Server server) {
this.socket = socket;
this.server = server;
this.isConnected = true;
this.in = null;
this.out = null;
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while (isConnected) {
String recv = in.readLine().trim();
if (recv != null) {
String[] data = recv.split("%");
}
}
} catch (IOException e) {}
}
public synchronized void send(String data) {
out.println(data);
}
}
您想要的是使用 下面是如何使用命令模式来处理来自客户端的命令
/* Command Exception. */
public class ClientCommandException extends Exception {
public ClientCommandException(String msg) {
super(msg);
}
}
/* The ClientCommand interface */
public interface ClientCommand {
void execute(Client client, String[] params) throws ClientCommandException;
}
import java.util.HashMap;
import java.util.Arrays;
/* Container for commands */
public class Commands implements ClientCommand {
private HashMap<String, ClientCommand> cmds;
public Commands() {
cmds = new HashMap<String, ClientCommand>();
}
public void addCommand(ClientCommand cmd, String name) {
cmds.put(name, cmd);
}
public void execute(Client client, String[] params) throws ClientCommandException {
ClientCommand cmd = cmds.get(params[0]);
if(cmd != null) {
cmd.execute(client, Arrays.copyOfRange(params, 1, params.length));
} else {
throw new ClientCommandException("Unknown Command: " + params[0]);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Client implements Runnable {
private boolean isConnected;
private Commands cmds;
private BufferedReader in;
private PrintWriter out;
private class EchoCommand implements ClientCommand {
public void execute(Client client, String[] params) throws ClientCommandException {
StringBuilder b = new StringBuilder("Echo back:");
int len = params.length;
for(int i = 0; i < len; i++) {
b.append(' ');
b.append(params[i]);
}
client.send(b.toString());
}
}
private class DisconnectCommand implements ClientCommand {
public void execute(Client client, String[] params) throws ClientCommandException {
client.close();
}
}
public Client() {
cmds = new Commands();
cmds.addCommand(new EchoCommand(), "echo");
cmds.addCommand(new DisconnectCommand(), "disconnect");
/* sub-commands */
Commands server = new Commands();
server.addCommand(new EchoCommand(), "print");
cmds.addCommand(server, "server");
isConnected = true;
}
public void addCommand(ClientCommand cmd, String name) {
cmds.addCommand(cmd, name);
}
public void close() {
isConnected = false;
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out, true);
while (isConnected) {
String recv = in.readLine().trim();
if (recv != null) {
String[] data = recv.split("%");
try {
cmds.execute(this, data);
} catch(ClientCommandException e) {
/* Return some error back to the client. */
out.println(e.toString());
}
}
}
} catch (IOException e) {}
}
public synchronized void send(String data) {
out.println(data);
}
public static void main(String[] args){
Client client = new Client();
System.out.println("Start Client.");
client.run();
}
}
/*命令异常*/
公共类ClientCommandException扩展了异常{
公共ClientCommandException(字符串消息){
超级(味精);
}
}
/*ClientCommand接口*/
公共接口ClientCommand{
void execute(客户端,字符串[]参数)抛出ClientCommandException;
}
导入java.util.HashMap;
导入java.util.array;
/*命令容器*/
公共类命令实现ClientCommand{
私有HashMap cmds;
公共命令(){
cmds=newhashmap();
}
public void addCommand(ClientCommand cmd,字符串名){
cmds.put(名称,cmd);
}
public void execute(客户端,字符串[]参数)引发ClientCommandException{
ClientCommand cmd=cmds.get(参数[0]);
如果(cmd!=null){
cmd.execute(client,Arrays.copyOfRange(params,1,params.length));
}否则{
抛出新的ClientCommandException(“未知命令:+params[0]);
}
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
公共类客户端实现可运行{
私有布尔不连通;
专用命令cmds;
中的私有缓冲区读取器;
私人打印输出;
私有类EchoCommand实现ClientCommand{
public void execute(客户端,字符串[]参数)引发ClientCommandException{
StringBuilder b=新的StringBuilder(“回显:”);
int len=参数长度;
对于(int i=0;i