📄 chatprotocol.java
字号:
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatProtocol
{
private String operator = null;
private String parameter = null;
private String content = null;
private void parseChatProtocol(String theInput)
{
try
{
theInput = theInput.trim();
//System.out.println(theInput);
int index1 = theInput.indexOf(";");
int index2 = theInput.indexOf(";",index1+1);
System.out.println(index1+" "+index2);
operator = theInput.substring(0,index1).trim();
System.out.println("operator:"+operator);
parameter = theInput.substring(index1+1,index2).trim();
System.out.println("parameter:"+parameter);
content = theInput.substring(index2+1).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("invail login format");
}
else
{
angent.userName = content;
ChatServer.broadcast.addUser(angent);
ChatServer.broadcast.sendTo(angent.userName + " login sucess!!",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;
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();
}
if(toPeople.equalsIgnoreCase(""))
{
toPeople = "all";
}
if(chatType.equalsIgnoreCase("private"))
{
if(toPeople.equalsIgnoreCase("all"))
{
theOutput = userName + " say to everybody :" + theOutput;
ChatServer.broadcast.broadcast(theOutput);
}
else
{
theOutput = userName + " say to "+toPeople+" :" + 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;
ChatServer.broadcast.broadcast(theOutput);
}
else
{
theOutput = userName + " say to "+toPeople+" :" + theOutput;
ChatServer.broadcast.broadcast(theOutput);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -