在Java中向socketServer通知GUI中发生的事件
我有一个接受客户端连接的socketServer。每次建立新连接时,我都会启动一个新线程来处理该连接 现在,我想通知所有客户机GUI中发生的事件。我尝试使用接口回调socketServer,但服务器的字段似乎为空在Java中向socketServer通知GUI中发生的事件,java,callback,Java,Callback,我有一个接受客户端连接的socketServer。每次建立新连接时,我都会启动一个新线程来处理该连接 现在,我想通知所有客户机GUI中发生的事件。我尝试使用接口回调socketServer,但服务器的字段似乎为空 public interface MyInterface { void actionPerf (String s); } public class FLoorPlan { public MyInterface listener; public FloorPl
public interface MyInterface {
void actionPerf (String s);
}
public class FLoorPlan {
public MyInterface listener;
public FloorPlan(MyInterface l) {
listener = l;
}
public FloorPlan(int init) {
//init the object as part of the GUI
}
public void notifyServer() {
listener.actionPerf("notif");
}
}
public class ServerNew implements MyInterface {
public ArrayList<MultiServerThreadRead> clients = new ArrayList<>();
public String testString;
public FloorPlan reference;
public ServerNew() {
reference = new FloorPlan(this);
}
public ServerNew(String s) {
int portNumber = 8511;
serverSocket = new ServerSocket(portNumber);
start2();
}
public void start2() throws IOException {
boolean listening = true;
testString = "modified";
try {
while (listening) {
clients.add(new MultiServerThreadRead(serverSocket.accept()));
clients.get(clients.size() - 1).start();
}
} catch (IOException e) {
System.err.println("Could not listen on port ");
System.exit(-1);
}
}
@Override
public void actPerf(String s) {
System.out.println(testString);// prints null
//notify the clients
}
}
我可以在ServerNew对象中触发事件,但显然它不是正确的对象,因为打印时testString字段为null。
我只需要一点提示,说明我的设计有什么问题,或者我是否对回调及其工作方式有错误的理解。谢谢 @Override public void actionPerf()--应该是public void actionPerf(字符串)吗?我输入了它,很抱歉,谢谢
public FloorPlan(int init) {
listener = new ServerNew();
//init the object as part of the GUI
}