JAVA入门到精通-第91讲-山寨QQ项目5-一对一的聊天
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004618218-2091111187.png)
聊天:
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004618613-274653573.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004618800-1984450689.png)
希望在界面最上面显示我的QQ号码:显示自己的编号;
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004619015-703850699.png)
在窗口栏显示自己的编号:
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004619407-610449590.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004619707-1026451530.png)
--------------------
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004620087-663580296.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004620675-703736359.png)
String ownerId
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004620891-1246931082.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004621063-345794662.png)
---------------------------------
真实的QQ,
客户端1/2可能直接走局域网;
借助于服务器进行中转:
客户端1如果登录成功,Hello,准备和2聊天,
信息发到服务器:
(1)如果2不在线,服务器策略:把数据暂存在数据库-message表;
等2上线,服务器把信息按顺序回送给客户端2;
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004621652-896363661.png)
(2)如果2在线,服务器和客户端2存在着一个连接,S,通道;
服务器策略:服务器为了可以完成信息转发,把服务器得到的每一个
socket[]保存在Hashmap里面;
对于每一个socket,服务器认为是一个线程,
当服务器和某个客户端形成一个连接时(Socket),
服务器端就单开一个线程;
让该线程与客户端通讯;
----------------------------------------------------
对服务器端进行改造:
单开一个线程,让该线程与客户端保持通讯:
-SerConClientThread
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004621927-1806586884.png)
public SerConClientThread(Socket s)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004622721-255894664.png)
把服务器和客户端的连接赋给s
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004622969-912791.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004623259-44581471.png)
读的是Message,通过 Message对象进行交互的;
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004623515-1264519369.png)
服务器得到的信息Message;
-----------------------
-Message属性的添加:
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004623742-1553589228.png)
发送者,接收者,信息内容,时间等等;
-----------------------------------
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004624000-1075598884.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004624740-217416170.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004625020-1802168178.png)
-----------------------------------------------------------
-----------------------------------------------------------
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004625248-42996266.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004625532-676570313.png)
new java.util.Date( ).toString( )
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004625753-935873551.png)
s要在QqChat里面运用;
//发送给服务器
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004626008-1667600849.png)
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004626259-1086321023.png)
---------------
---------------
-线程控制的,这条通道已经打通了;
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004627683-1038959277.png)
-2号在线,1号发给2号的信息,通过服务器转给2号 ;
-服务器只要拿到socket,就好了;
服务器和getter达成的socket;
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004628567-434150037.png)
-我们可以把服务器得到的每个socket[客户端],
保存在Hashmap,用客户的id号标识该socket;
-ManageClientThread
Hashmap<String,SerConClientThread> ();
String 用户ID;
SerConClientThread 用户线程;
只能有一份HashMap;
-把线程加入HashMap里面
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004630117-1574322896.png)
-转发是在线程当中发生的
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004630579-2140619744.png)
//
//
接收到某个人给某个人发的message对象,
找到接收人的那个通讯线程,
再将其发回去;
//
----------
QqChat必须一直要处于读的状态,做成线程;
才能进行聊天
![](https://img2018.cnblogs.com/blog/1167156/201812/1167156-20181201004632598-1796389497.png)
ObjectInputStream ois=new Object
转发任务已经完全实现了;
-----------------------------------
好友在线,显示彩色头像;
反之,显示灰色头像;