首页>代码>java swing聊天室程序--带语音>/局域网聊天-带语音/ChatClient/src/Listener.java
import java.awt.List;
import java.io.*;
import java.util.StringTokenizer;
/*
 * 该类实现对服务器发送的消息进行监听
 */
public class Listener extends Thread 
{
    BufferedReader br;
    List onlineUsersList,chatContentList;
    boolean isClosed=false;
    FileSender fileSender;				//文件传输使用
    FileReceiver fileReceiver;
    VoiceReceive voiceReceive;
    VoiceSend voiceSend;
    public Listener(BufferedReader br,List onLineUsersList,List chatContentList) 
    {
        this.br=br;
        this.onlineUsersList=onLineUsersList;
        this.chatContentList=chatContentList;
        this.start();
    }
    public void run()
    {
        String word=null;
        try {
            //循环接收服务器端发送来的信息
            while (!isClosed && (word = br.readLine()) != null)
            {
                //判断其是否是更新在线用户列表的信息
                //已修改。由NICKNAME开头,不再有以ONLINEUSERS开头的消息
                if(word.startsWith("NICKNAME:"))
                {
                    word=word.substring(9);
                    onlineUsersList.removeAll();
                    StringTokenizer st = new StringTokenizer(word);
                    while (st.hasMoreTokens()) 
                    {
                        onlineUsersList.add(st.nextToken());
                    }
                }
                //得到服务器端相应过来的目标用户的ip地址
                else if(word.startsWith("FILEECHO:"))
                {
                    word=word.substring(9);
                    this.fileSender.setDescIP(word);
                }
                else if(word.startsWith("FLENGTH:"))
                {
                	String filelength=word.substring(8);
                	ClientFrame.filelength=Long.parseLong(filelength);
                	ClientFrame.bar.setMinimum (0);
                	ClientFrame.bar.setMaximum((int)(ClientFrame.filelength/1024));
                }
                else if(word.startsWith("VOICEECHO:"))
                {
                	word=word.substring(10);
                	this.voiceSend.setIP(word);
                	ClientFrame.voiceSend.start();
                	ClientFrame.voiceReceive.start();
                	ClientFrame.jButton13.setText("结束语音");
                	ClientFrame.jButton13.setVisible(true);
                	ClientFrame.jButton12.setVisible(false);
                	this.chatContentList.add("你与"+onlineUsersList.getSelectedItem()+"开始语音聊天!");
                }
                else if(word.startsWith("VOICE:"))
                {
                	ClientFrame.jButton13.setText("结束语音");
                	ClientFrame.jButton13.setVisible(true);
                	ClientFrame.jButton14.setVisible(true);
                	ClientFrame.jButton12.setVisible(false);
                	word=word.substring(6);
                	ClientFrame.VSip=word;
                	this.chatContentList.add("你与"+onlineUsersList.getSelectedItem()+"开始语音聊天!");
                }
                else if(word.startsWith("VOICEEND:"))
                {
                	ClientFrame.jButton12.setVisible(true);
            		ClientFrame.jButton13.setVisible(false);
            		ClientFrame.jButton14.setVisible(false);
            		ClientFrame.jButton13.setText("拒绝语音");
                	ClientFrame.voiceSend.stop();
                	ClientFrame.voiceReceive.stop();
                	ClientFrame.VSip="";
                	ClientFrame.jButton14.setVisible(false);
                	this.chatContentList.add("你与"+onlineUsersList.getSelectedItem()+"聊天结束!");
                }
                else
                {
                    //否则就将其打到交谈信息里
                    this.chatContentList.add(word);
                }
            }
        } 
        catch (IOException ex) 
        {
            isClosed=true;
        }
    }
    public void destroy()
    {
        try 
        {
            isClosed=true;
        } 
        catch (Exception ex)
        {
        }
    }
    public void setFileSender(FileSender fs)
    {
        this.fileSender=fs;
    }
    public void setFileReceiver(FileReceiver fr)
    {
        this.fileReceiver=fr;
    }
    public void setVoiceSend(VoiceSend vs)
    {
    	this.voiceSend=vs;
    }
    public void setVoiceReceive(VoiceReceive vr)
    {
    	this.voiceReceive=vr;
    }
}
最近下载更多
微信网友_6444139264921600  LV6 2023年4月29日
Sopuding  LV1 2022年7月16日
tomtom113  LV2 2022年6月9日
ewan007  LV29 2022年4月21日
tdcq123  LV14 2022年3月13日
双方各何必呢  LV13 2021年12月20日
曹思辰  LV6 2021年11月21日
阳光正好  LV2 2021年10月29日
1005948011  LV7 2021年6月1日
服气辣  LV2 2021年5月24日
最近浏览更多
1112WHQ  LV7 2023年11月3日
yuchunxing  LV1 2023年9月2日
2017143155  LV12 2023年6月24日
lynn_zhou  LV1 2023年5月30日
微信网友_6444139264921600  LV6 2023年4月29日
生米的  LV10 2023年3月3日
ann000  LV2 2022年12月7日
queueandstack  LV3 2022年12月1日
微信网友_6229090122354688 2022年11月21日
暂无贡献等级
xinxingyuan8 2022年9月29日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友