8:30-9:00 学习打卡
9:30-12:00 写qq项目
15:00-17:00 写qq项目
1.qq离线接收类
接收qq离线消息,以qq账号读出,消息组为arraylist保存,等上线就读出。
package qqserver.mode;
import qqc.massage.Massage;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
public class MassageDemo {
public static HashMap<String, ArrayList<Massage>> hm=new HashMap<>();
public static void add(String id,ArrayList<Massage> m){
hm.put(id,m);
}
public static ArrayList<Massage> getMassage(String id){
return (ArrayList<Massage>) hm.get(id);
}
public static void Massagecate(String id){
ArrayList<Massage> ms=MassageDemo.getMassage(id);
ObjectOutputStream oos=null;
for(Massage m:ms){
try {
oos = new ObjectOutputStream(Mythreads.getthread(id).s.getOutputStream());
oos.writeObject(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.改进消息接收类
消息接收如果不在线保存到离线接收类里面
package qqserver.mode;
import qqc.massage.Massage;
import qqserver.view.qqserverv;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class Mythread extends Thread {
public Socket s;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
public Mythread(Socket ss) {
this.s = ss;
}
@Override
public void run() {
while(true){
try {
ois=new ObjectInputStream(s.getInputStream());
Massage m= (Massage) ois.readObject();
if(m.getS().equals("1")) {//好友信息
System.out.println(m.getMyid() + "对" + m.getOid() + "说: " + m.getXiaoxi());
Mythread mt = Mythreads.getthread(m.getOid());
if(mt!=null){
oos = new ObjectOutputStream(mt.s.getOutputStream());
oos.writeObject(m);
}
else{//不在线保存在离线信息接收
if(MassageDemo.hm.containsKey(m.getOid())){
ArrayList<Massage> ms= MassageDemo.getMassage(m.getOid());
ms.add(m);
}
else{
ArrayList<Massage> ms=new ArrayList<Massage>();
ms.add(m);
MassageDemo.add(m.getOid(),ms);
}
}
}
else if(m.getS().equals("2")){//接收离线消息
if(MassageDemo.hm.containsKey(m.getMyid()))
{
MassageDemo.Massagecate(m.getMyid());
}
}
else if(m.getS().equals("4")){//群消息
Mysql my=new Mysql();
System.out.println(m.getQun());
String[] s1=my.qun("1",m.getQun()).split("\\|");
for(int i=0;i<s1.length;i++){
Massage ms=new Massage();
ms.setMyid(m.getMyid());
ms.setQun(m.getQun());
ms.setS("4");
ms.setXiaoxi(m.getXiaoxi());
ms.setOid(s1[i]);
if(Mythreads.hm.containsKey(s1[i])){
ObjectOutputStream oos2 = new ObjectOutputStream(Mythreads.getthread(s1[i]).s.getOutputStream());
ms.setOid(s1[i]);
oos2.writeObject(ms);
}
else {//不在线保存在离线消息接收类
if(MassageDemo.hm.containsKey(s1[i])){
ArrayList<Massage> mms= MassageDemo.getMassage(s1[i]);
mms.add(ms);
}
else{
ArrayList<Massage> mms=new ArrayList<Massage>();
mms.add(ms);
MassageDemo.add(s1[i],mms);
}
}
}
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}