Flex和java的socket通信(五)聊天室的补充-在线列表-私聊
服务端代码:Server5.java
- /*
- * 自定义的协议
- * 收到消息: 11开头表示新加入了聊天用户;22开头表示公聊;33开头表示私聊
- * 发送消息:11开头表示更新用户列表;22开头表示发送到屏幕上;44发送在线人数
- *
- */
- import java.net.*;
- import java.io.*;
- import java.util.*;
- public class Server5 {
- private ServerSocket server;
- private BManager bMan=new BManager(); //消息广播者
- //Map接口的HashMap类,元素拥有固定key值,key值不重复,这里用来存放在线用户
- Map<Socket,String> clientList = new HashMap<Socket,String>();
- public Server5(){} //构造函数
- void startServer() //启动服务器
- {
- try{
- server=new ServerSocket(8888); //创建服务器套接字
- System.out.println("服务器套接字建立完毕");
- while(true)
- {
- Socket socket=server.accept(); //若客户机提出请求,使用socket进行连接
- //String strIP = socket.getInetAddress().toString();//登陆者的ip
- Chat_Thread ct=new Chat_Thread(socket);
- ct.start(); //启动线程
- bMan.add(socket); //添加套接字
- bMan.sendClientInfo();//使用套接字输出当前聊天人数
- //funList(clientList);
- //bMan.sendToAll(strIP+"/加入聊天室");
- }
- }catch(Exception e){
- System.out.println(e);
- }
- }
- public static void main(String[] args) {
- Server5 server=new Server5();
- server.startServer();
- }
- class Chat_Thread extends Thread //与客户机进行通讯的线程类
- {
- Socket socket;//x1
- private BufferedReader reader; //套接字输入流;
- private PrintWriter writer; //套接字输出流
- Chat_Thread(Socket socket)
- {
- this.socket=socket;//this.socket就是x1处的socket
- }
- public void run()
- {
- try
- {
- reader=new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf8"));
- writer=new PrintWriter(socket.getOutputStream(),true);
- String msg;
- //msg获取消息
- while((msg=reader.readLine())!=null)
- {
- System.out.println(msg);//服务器屏幕输出消息
- String str=msg.substring(0, 2);//截取前两个个字符
- int a=Integer.parseInt(str);//强制转换成int
- String[] arrMsg=msg.split("--");//将获取的消息以"--"符号为标志分解成数组
- switch(a)
- {
- case 11 : //当消息以11开头的时候,将登陆者的信息储存到hashmap之中,并向客户端发送新的在线列表
- String strName=msg.substring(2);//获取登陆者名字,消息格式“11eko”
- System.out.println(strName+"登陆了");//服务器屏幕输出谁登陆了
- bMan.sendToAll("22"+strName+"登陆了");//广播谁登陆了
- clientList.put(this.socket,strName);//加入到HashMap中
- funList(clientList);//广播在线列表
- break;
- case 22://当消息以22开头的时候,内容为“22--eko--内容”
- System.out.println("公聊");
- //构造消息,arrMsg[0]=消息头,arrMsg[1]消息发送者,arrMsg[2]消息内容
- msg=arrMsg[0]+arrMsg[1]+"说:"+arrMsg[2];
- bMan.sendToAll(msg);//向所有人广播消息
- break;
- case 33://消息以33开头时候,内容为“33--sandal--eko--内容”
- //arrMsg[1]为说话对象,arrMsg[2]为说话人,arrMsg[3]为消息内容
- if(arrMsg[1].equals("所有人"))//当说话对象为"所有人"的时候
- {
- //构造消息"22eko说:内容"
- msg="22"+arrMsg[2]+"说:"+arrMsg[3];
- //向所有人发送消息
- bMan.sendToAll(msg);
- }else //其他情况就是向具体的某个人发送消息了
- {
- Socket socketOne;
- System.out.println("私聊");
- Set set = clientList.keySet();//使用keySet方法获取所有key值
- Iterator it = set.iterator();//使用Iterator(迭代器)来遍历数据
- while (it.hasNext()) { //返回是否还有没被访问过的对象
- Object ok=it.next();//返回下一个没被访问过的对象
- Object ov=clientList.get(ok);//get方法返回拥有key的元素
- if(ov.equals(arrMsg[1]))//如果在client中找到"消息发给谁"的时候,发给对方
- {
- socketOne=(Socket)ok;//强制转换成key值类型;
- bMan.sendToONE(socketOne,"22(悄悄话)"+arrMsg[2]+"对你说:"+arrMsg[3]);
- }else if(ov.equals(arrMsg[2]))//如果在client中找到"发消息的人"的时候,发给他自己
- {
- socketOne=(Socket)ok;
- bMan.sendToONE(socketOne,"22(悄悄话)你对"+arrMsg[1]+"说:"+arrMsg[3]);
- }
- }
- }
- break;
- }
- //bMan.sendToAll(msg);
- }
- }catch(Exception e)
- {
- }finally
- {
- try {
- bMan.remove(socket);
- if(reader !=null) reader.close();
- if(writer !=null) writer.close();
- if(socket !=null) socket.close();
- if(clientList.containsKey(socket))
- {
- bMan.sendToAll("22"+clientList.get(socket)+"离开了...");//广播消息,谁离开了
- clientList.remove(socket);//删除socket
- funList(clientList);//广播在线列表
- }
- reader=null;
- writer=null;
- socket=null;
- System.out.println("客户机离开");
- bMan.sendClientInfo();//广播在线人数
- } catch (Exception e) {}
- }
- }
- }
- void funList(Map clientList) // 广播在线列表
- {
- String strList="";//在线列表
- Set set = clientList.keySet();//使用keySet方法获取所有key值
- System.out.println(set);
- Iterator it = set.iterator();//使用Iterator(迭代器)来遍历数据
- System.out.println(it);
- while (it.hasNext()) {//把用户名称发给在线所有客户端
- //构造在线列表格式strList=11--one--two--three
- strList+="--";
- strList+=clientList.get(it.next());
- }
- bMan.sendToAll("11"+strList);
- }
- }
- class BManager extends Vector
- {
- BManager (){}
- void add(Socket sock)
- {
- super.add(sock);
- }
- void remove(Socket sock)
- {
- super.remove(sock);
- }
- synchronized void sendToAll(String msg)//给所有人广播函数
- {
- PrintWriter writer=null;
- Socket sock;
- for(int i=0;i<size();i++) //执行循环
- {
- sock=(Socket)elementAt(i);//获取第i个套接字
- try
- {
- //获取第i个套接字输出流
- writer=new PrintWriter(sock.getOutputStream(),true);
- }catch(Exception ie){}
- //使用第i各套接字输出流,输出消息
- if(writer!=null)writer.println(msg);
- }
- }
- synchronized void sendToONE(Socket socket,String msg)//私聊函数
- {
- PrintWriter writer=null;
- Socket sock;
- for(int i=0;i<size();i++)
- {
- sock=(Socket)elementAt(i);
- if(socket==sock)//与给所有人广播函数类似,仅加入了判断,只有当socket管理器中的socket等于传入的socket的时候才发送消息
- {
- try
- {
- writer=new PrintWriter(sock.getOutputStream(),true);
- }catch(Exception ie){}
- if(writer!=null)writer.println(msg);
- }
- }
- }
- synchronized void sendClientInfo()
- {
- String info="44当前聊天人数:"+size();
- //System.out.println(info);
- sendToAll(info);
- }
- }
知识点:
实现在线列表的思路:每当有新的用户登陆的时候就像服务器发送用户名,服务器收到用户明后就会存在clientList中,然后再提取所有的用户名广播出去。
实现私聊的思路,利用hashmap来储存数据,通过name找到socket,然后把消息发给找到的socket
关于map,首先就要说说Collection接口,它位于与数据结构有关的
API
的最上部。构成Collection的单位,我们称之为元素(element),此接口提供了添加,删除元素等管理数据的功能,根据管理方法的不同,可将Collection接口分为Set,List,Map三种接口。
实现Map接口的类也有3个,分别是HashMap类,TreeMap类和Hashtable类。这些类具有如下几个特征:元素拥有固定的key值;key值不允许重复。
下面就说说这次说用道德HashMap类的常用方法。
添加一个拥有key的元素:put(Object key,Object value);
删除拥有key的元素:remove(Object key);
返回拥有key的元素:get(Object key);
通过get方法可以通过key查找value,那反过来呢?如何通过HashMap中的value找到key。似乎并没有现成的方法。这里提供一个思路。关于关于Iterator和Set大家可以自行查查java文档,这里就浪费论坛的空间了。
- pritnkey(HashMap hm,String value)
- {
- ArrayList a=new Array();
- Set key =hm.keySet();
- Iterator it=key.iterator();
- while(it.hasNext())
- {
- Object ok=it.next();
- Object ov=hm.get(ok);
- if(ov.equals(value))
- {
- a.add(ok);
- }
- if(a.size()!=0)
- {
- System.out.println(a);
- }
- }
- }
客户端:myLogin.mxml;Client5.mxml
没什么好讲的,知识点在前四节中已经反复的提过了,这里就不再累叙了。
所要注意的无非就是通过字符串操作来分割服务器传来的消息,经过判断然后进行归类。
所要注意的一点是,在登陆的时候要做好验证工作,确保用户列表不重名。