📄 chatprotocol.java
字号:
package Section17;
import java.io.IOException;
public class ChatProtocol {
private String operator=null;
private String parameter=null;
private String content=null;
private void parseChatProtocol(String theInput)
{
try{
Exception e=new Exception();
theInput=theInput.trim();
int index1=theInput.indexOf(";");
if(index1<0)throw e;
int index2=theInput.trim().indexOf(";",index1+1);
if(index2<0)throw e;
operator=theInput.substring(0,index1);
System.out.println("operator:"+operator);
parameter=theInput.substring(index1+1,index2).trim();
System.out.println("parameter:"+parameter);
content=theInput.substring(index2+1,theInput.length()).trim();
System.out.println("content:"+content);
}catch(Exception e)
{
System.out.println("invail command format!");
//e.printStackTrace();
}
}
public Angent login(String theInput,Angent angent)
{
parseChatProtocol(theInput);
if(angent.userName==null)
{
if(content==null||content.equals("")||content.equalsIgnoreCase("all")){
System.out.println("invalid login format");
}
else{
angent.userName=content;
ChatServer.broadcast.addUser(angent);
ChatServer.broadcast.sendTo(angent.userName+" login success!!",angent.userName);
}
}
else{
if(operator.equalsIgnoreCase("logout"))angent.close();
}
return angent;
}
public void chat(String theInput,Angent angent)
{
parseChatProtocol(theInput);
String theOutput=content;
String userName=angent.userName;
System.out.println("the Angent's userName is: "+userName);
int index1=parameter.indexOf(" ");
String chatType="unKnown";
String toPeople="all";
if(!operator.equalsIgnoreCase("chat")){
theOutput="usage:[chat;private/public somebody;something you want to say...]";
angent.out.println(theOutput);
angent.out.flush();
return;
}
if(parameter.equals("")||index1==-1)
{
System.out.print("invail parameter");
return;
}
else{
chatType=parameter.substring(0,index1).trim();
toPeople=parameter.substring(index1+1).trim();
System.out.println("chatType: "+chatType+" to "+chatType);
}
if(toPeople.equalsIgnoreCase(""))
{
toPeople="all";
}
if(chatType.equalsIgnoreCase("private"))
{
if(toPeople.equalsIgnoreCase("all"))
{
theOutput=userName+" say to everybody: "+theOutput;
System.out.println(theOutput);
ChatServer.broadcast.broadcast(theOutput);
}
else
{
theOutput=userName+" say to "+toPeople+": "+theOutput;
System.out.println(theOutput);
ChatServer.broadcast.sendTo(theOutput,toPeople);
ChatServer.broadcast.sendTo(theOutput,userName);
}
}
if(chatType.equalsIgnoreCase("public")){
if(toPeople.equalsIgnoreCase("all")){
theOutput=userName+" say to everybody:"+theOutput;
System.out.println(theOutput);
ChatServer.broadcast.broadcast(theOutput);
}
else{
theOutput=userName+" say to "+toPeople+" : "+theOutput;
System.out.println(theOutput);
ChatServer.broadcast.broadcast(theOutput);
}
}
theOutput=userName;
angent.out.print(theOutput+": ");
angent.out.flush();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -