⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listener.java

📁 使用java语言写的聊天工具
💻 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;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -