我正在用java編程一個在線視頻遊戲。我已經完成了服務器,現在我進入了客戶端。 我的問題在於socket偵聽器代碼中的某個地方,這是一個swingworker子類,它的任務是偵聽服務器(doInBackGround())並根據需要更新遊戲地圖。Java Swing中的差異發佈()/ process()交互
下面的代碼:
import javax.swing.*;
import java.util.List;
public class GameWorker extends SwingWorker<Void, String> {
private SocketStreamsBean streams;
private GameFrame game;
public GameWorker(SocketStreamsBean streams, GameFrame game) {
this.streams = streams;
this.game = game;
}
@Override
protected Void doInBackground() throws Exception {
for(String msg = streams.getIn().readLine(); msg != null; msg = streams.getIn().readLine()){
System.out.println("bp " + msg + " " + Thread.currentThread().getId());//TODO remove
publish(msg);
System.out.println("ap " + msg + " " + Thread.currentThread().getId());//TODO remove
}
return null;
}
@Override
protected void process(List<String> list) {
for(String msg = list.remove(0); list.size() != 0; msg = list.remove(0)) {
System.out.println("dp " + msg + " " + Thread.currentThread().getId());//TODO remove
String[] cmds = msg.split(":");
switch (cmds[0]) {
case "ADD":
game.add(cmds[1], cmds[2], cmds[3]);
break;
case "MOVE":
game.remove(cmds[1]);
game.add(cmds[1], cmds[2], cmds[3]);
break;
case "REMOVE":
game.remove(cmds[1]);
break;
case "BULLETS":
//game.addBullets(cmds[1]);
}
}
list.clear();
}
}
據三米調試的println()當玩家移動和服務器廣播它的信息被讀取並公佈,但從來沒有處理的所有客戶端。怎麼會這樣?
不要從內部列表中刪除過程方法。簡單地迭代和處理。 –