如何使用Java超时套接字仅读取来自服务器的最后一条消息
我正在写一个小游戏使用处理,并希望添加一个多人游戏功能。我使用了Socket,但现在有一个问题:我只想读取来自服务器的最后一条消息,并使用一些不空闲的消息作为服务器消息。这是我使用的代码。这就像服务器的关卡选择,它将选择的关卡发送给客户端,以便它可以在客户端加载,因为服务器只在加载新关卡时发送消息,客户端的游戏只是冻结,直到它收到来自服务器的消息 服务器的代码:如何使用Java超时套接字仅读取来自服务器的最后一条消息,java,processing,serversocket,Java,Processing,Serversocket,我正在写一个小游戏使用处理,并希望添加一个多人游戏功能。我使用了Socket,但现在有一个问题:我只想读取来自服务器的最后一条消息,并使用一些不空闲的消息作为服务器消息。这是我使用的代码。这就像服务器的关卡选择,它将选择的关卡发送给客户端,以便它可以在客户端加载,因为服务器只在加载新关卡时发送消息,客户端的游戏只是冻结,直到它收到来自服务器的消息 服务器的代码: //Set up all the buttons disableMenus(); enableMenus(7,
//Set up all the buttons
disableMenus();
enableMenus(7,13);
enableMenus(19,19);
//show the current loaded level
printLevel();
printGuards(true);
for(int i = 0 ; i < cams.length ; i++) {
if(cams[i]) {
while (camAngleMin[i] > camAngleMax[i]) {
camAngleMin[i] = camAngleMin[i] - 2*PI;
}
int t = 0;
int r = 0;
int buffer = round(camAngle[i]/(PI/2));
PImage images;
if(buffer > 3)
buffer = 0;
else if(buffer < 0)
buffer = 3;
images = camImage[buffer];
if(buffer == 0) {
t = images.width;
r = 1;
} else if(buffer == 1) {
t = images.width/2;
r = images.height;
} else if(buffer == 2) {
t = 0;
r = 1;
} else {
t = images.width/2;
r = 0;
}
if(camAngle[i] > camAngleMax[i] && camBuffer[i] == 0) {
camGoToMin[i] = true;
camBuffer[i] = 200;
} else if(camAngle[i] < camAngleMin[i] && camBuffer[i] == 0) {
camGoToMin[i] = false;
camBuffer[i] = 200;
} else if(camBuffer[i] > 0){
camBuffer[i]--;
}
//println(camGoToMin[i]+":"+camBuffer[i]);
if(camGoToMin[i] && camBuffer[i] == 0) {
camAngle[i] = camAngle[i] - (PI/512);
}
else if(camBuffer[i] == 0){
camAngle[i] = camAngle[i] + (PI/512);
}
image(images,camX[i],camY[i]);
//lumiereCam[i].display(camX[i]+t, camY[i]+r,false,null,camAngle[i]);
rayC[i].display(camX[i]+t, camY[i]+r, str(camAngle[i]));
}
//Send the current loaded to the client
if(!compareStrings(loadedLevel,lastLevelLoaded)) {
dout.println(loadedLevel);
lastLevelLoaded = loadedLevel;
}
dout.flush();
}
//设置所有按钮
残疾();
使能(7,13);
使能(19,19);
//显示当前加载的级别
printLevel();
printGuards(真实);
对于(int i=0;icamAngleMax[i]){
camAngleMin[i]=camAngleMin[i]-2*PI;
}
int t=0;
int r=0;
int缓冲区=圆形(凸轮角度[i]/(PI/2));
图像;
如果(缓冲区>3)
缓冲区=0;
else if(缓冲区<0)
缓冲区=3;
图像=camImage[缓冲区];
如果(缓冲区==0){
t=图像宽度;
r=1;
}else if(缓冲区==1){
t=图像宽度/2;
r=图像。高度;
}else if(缓冲区==2){
t=0;
r=1;
}否则{
t=图像宽度/2;
r=0;
}
如果(camAngle[i]>camAngleMax[i]&&camBuffer[i]==0){
camGoToMin[i]=真;
坎布弗[i]=200;
}else if(camAngle[i]0){
坎布弗;
}
//println(camGoToMin[i]+“:”+camBuffer[i]);
if(camGoToMin[i]&camBuffer[i]==0){
凸轮角度[i]=凸轮角度[i]-(PI/512);
}
else if(camBuffer[i]==0){
凸轮角度[i]=凸轮角度[i]+(PI/512);
}
图像(图像,camX[i],camY[i]);
//显示(camX[i]+t,camY[i]+r,false,null,camgangle[i]);
rayC[i].显示(camX[i]+t,camY[i]+r,str(camgangle[i]);
}
//将当前加载的数据发送到客户端
如果(!compareStrings(loadedLevel,lastLevelLoaded)){
dout.println(加载级别);
lastLevelLoaded=loadedLevel;
}
dout.flush();
}
客户机代码:
try {
//request the level loaded by the server
String level = din.readLine();
//set up the buttons
disableMenus();
//print the current loaded level on the screen
printLevel();
printGuards(true);
for(int i = 0 ; i < cams.length ; i++) {
if(cams[i]) {
while (camAngleMin[i] > camAngleMax[i]) {
camAngleMin[i] = camAngleMin[i] - 2*PI;
}
int t = 0;
int r = 0;
int buffer = round(camAngle[i]/(PI/2));
PImage images;
if(buffer > 3)
buffer = 0;
else if(buffer < 0)
buffer = 3;
images = camImage[buffer];
if(buffer == 0) {
t = images.width;
r = 1;
} else if(buffer == 1) {
t = images.width/2;
r = images.height;
} else if(buffer == 2) {
t = 0;
r = 1;
} else {
t = images.width/2;
r = 0;
}
if(camAngle[i] > camAngleMax[i] && camBuffer[i] == 0) {
camGoToMin[i] = true;
camBuffer[i] = 200;
} else if(camAngle[i] < camAngleMin[i] && camBuffer[i] == 0) {
camGoToMin[i] = false;
camBuffer[i] = 200;
} else if(camBuffer[i] > 0){
camBuffer[i]--;
}
//println(camGoToMin[i]+":"+camBuffer[i]);
if(camGoToMin[i] && camBuffer[i] == 0) {
camAngle[i] = camAngle[i] - (PI/512);
}
else if(camBuffer[i] == 0){
camAngle[i] = camAngle[i] + (PI/512);
}
image(images,camX[i],camY[i]);
//lumiereCam[i].display(camX[i]+t, camY[i]+r,false,null,camAngle[i]);
rayC[i].display(camX[i]+t, camY[i]+r, str(camAngle[i]));
}
}
//check if the game have to be launched
if(compareStrings(level,"start")) {
menu = 11;
}
//check if the server's level is already loaded
else if(!compareStrings(loadedLevel,level) && level != null && level.length() != 0) {
loadLevelPath(level);
}
} catch(SocketException e) {
menu = -1;
errorMsg("Le serveur à quitté",Error);
}
试试看{
//请求服务器加载的级别
字符串级别=din.readLine();
//设置按钮
残疾();
//在屏幕上打印当前加载的级别
printLevel();
printGuards(真实);
对于(int i=0;icamAngleMax[i]){
camAngleMin[i]=camAngleMin[i]-2*PI;
}
int t=0;
int r=0;
int缓冲区=圆形(凸轮角度[i]/(PI/2));
图像;
如果(缓冲区>3)
缓冲区=0;
else if(缓冲区<0)
缓冲区=3;
图像=camImage[缓冲区];
如果(缓冲区==0){
t=图像宽度;
r=1;
}else if(缓冲区==1){
t=图像宽度/2;
r=图像。高度;
}else if(缓冲区==2){
t=0;
r=1;
}否则{
t=图像宽度/2;
r=0;
}
如果(camAngle[i]>camAngleMax[i]&&camBuffer[i]==0){
camGoToMin[i]=真;
坎布弗[i]=200;
}else if(camAngle[i]0){
坎布弗;
}
//println(camGoToMin[i]+“:”+camBuffer[i]);
if(camGoToMin[i]&camBuffer[i]==0){
凸轮角度[i]=凸轮角度[i]-(PI/512);
}
else if(camBuffer[i]==0){
凸轮角度[i]=凸轮角度[i]+(PI/512);
}
图像(图像,camX[i],camY[i]);
//显示(camX[i]+t,camY[i]+r,false,null,camgangle[i]);
rayC[i].显示(camX[i]+t,camY[i]+r,str(camgangle[i]);
}
}
//检查是否必须启动游戏
if(比较(级别,“开始”)){
菜单=11;
}
//检查服务器的级别是否已加载
如果(!compareStrings(loadedLevel,level)&&level!=null&&level.length()!=0),则为else{
loadLevelPath(level);
}
}捕获(SocketException e){
菜单=-1;
errorMsg(“leserveuráquitté”,错误);
}
感谢您抽出时间回复我的帖子您想了解异步io吗。或者向客户端添加另一个线程。一个线程从服务器读取数据并将数据推送到某个队列中,另一个线程只检查该队列中是否有内容。但是请理解,你的问题可能太广泛了,因为有太多的方法来解决这个问题。你必须阅读服务器上的所有内容,并放弃你不想要的内容。