如何做出类似QQ的聊天系统,就是把聊天内容发到 客户端,不放在 服务器端,就是不保存,怎么做,我用的语言是java
交互是语言是jsp,请高手指教

解决方案 »

  1.   

    参考 webim据我初步了解:原理是用户的输入保存在服务器端,再由服务器发送到另一个客户端,通常用数据库作为中介
      

  2.   

    DWR 反向AJAX,不过这个是广播式的,如果要点对点的,要再做用户之类的处理。
      

  3.   

    不好做啊,以前就做过类似的,不过是保存在application中,但这样肯定不行,如果保存在数据库中有感觉太浪费资源,不好做啊
      

  4.   

    有QQ控制台的 没有jsp页面的
    参考一下
    import java.net.*;
    import java.io.*;
    import java.util.*;import javax.swing.*;
    import java.awt.event.*;
    public class QQClient {
    String name;
    BufferedReader in;
    PrintWriter out;
    JTextField jtf;
    JTextArea jta;
    JComboBox jcb;
    public static void main(String[] args) {
    if (args.length==0 || args[0].equals("All")
    ||args[0].indexOf(":")!=-1){
    System.out.println("Error!");
    System.exit(0);
    }
    Socket s=null;
    try {
    s = new Socket("127.0.0.1",7755);
    QQClient client=new QQClient(s,args[0]);
    client.prepare();
    client.receive();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public QQClient(Socket s,String name) throws Exception{
    this.name=name;
    this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
    this.out=new PrintWriter(s.getOutputStream());
    JFrame frame=new JFrame("Client "+name);
    frame.setSize(400,300);
    JPanel panel=new JPanel();
    this.jcb=new JComboBox();
    jcb.addItem("All");
    this.jtf=new JTextField(25);
    panel.add(jcb);
    panel.add(jtf);
    this.jta=new JTextArea();
    this.jta.setEditable(false);
    frame.getContentPane().add(new JScrollPane(jta));
    frame.getContentPane().add(panel,"South");
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent arg0) {
    end();
    }
    });

    this.jtf.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
    send();
    }
    });
    }
    void prepare() throws Exception{
    out.println("1:"+name+":");
    out.flush();
    }
    void end(){
    out.println("2:"+name+":");
    out.flush();

    System.exit(0);
    }
    void send(){
    String receiver=(String)(this.jcb.getSelectedItem());
    if (receiver==null) return;
    String text=this.jtf.getText();
    if ((text.indexOf(":"))!=-1){
    this.jtf.setText("");
    return;
    }
    out.println("3:"+receiver+":"+text);
    out.flush();
    this.jtf.setText("");
    }
    void receive(){
    while(true){
    try {
    String str=in.readLine();
    String[] info=parseString(str);
    if (info[0].equals("1")){
    this.jta.append(info[1]+" entered!"+"\n");
    this.jcb.addItem(info[1]);
    }
    else if (info[0].equals("2")){
    this.jta.append(info[1]+" exit!"+"\n");
    this.jcb.removeItem(info[1]);
    }
    else{
    this.jta.append(info[1]+" said:"+info[2]+"\n");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    private String[] parseString(String s){
    String[] ss=new String[3];
    int i=0;
    StringTokenizer st=new StringTokenizer(s,":");
    while(st.hasMoreTokens()){
    ss[i]=st.nextToken();
    i++;
    }
    return ss;
    } }import java.net.*;
    import java.io.*;
    import java.util.*;
    public class QQServer {
    public static void main(String[] args) {
    ServerSocket ss=null;
    Map users=new HashMap();
    try {
    ss = new ServerSocket(7755);
    } catch (IOException e) {
    e.printStackTrace();
    }
    while(true){
    try {
    Socket s=ss.accept();
    Thread t=new ServerThread(s,users);
    t.start();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }}class ServerThread extends Thread{
    Socket s;
    String userName;
    Map users;
    BufferedReader in;
    PrintWriter out;

    public ServerThread(Socket s,Map m){
    this.s=s;
    this.users=m;
    try {
    this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
    this.out=new PrintWriter(s.getOutputStream());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    public void run(){
    while(true){
    try {
    String str=in.readLine();
    if (str==null) break;
    String[] info=parseString(str);
    if (info[0].equals("1")){
    this.userName=info[1];
    send(str,"All");
    this.users.put(userName,out);
    Iterator it=users.keySet().iterator();
    while(it.hasNext()){
    String user=(String)(it.next());
    send("1:"+user+":",userName);
    }
    }
    else if (info[0].equals("2")){
    send(str,"All");
    this.users.remove(info[1]);
    break;
    }
    else{
    String text="3:"+this.userName+":"+info[2];
    send(text,info[1]);
    }
    }  catch (Exception e) {
    // e.printStackTrace();
    return;
    }
    }

    }
    private String[] parseString(String s){
    String[] ss=new String[3];
    int i=0;
    StringTokenizer st=new StringTokenizer(s,":");

    while(st.hasMoreTokens()){
    ss[i]=st.nextToken();
    i++;
    }
    return ss;
    }
    private void send(String text,String receiver) throws Exception{
    PrintWriter o;
    if (receiver.equals("All")){
    Iterator it=users.keySet().iterator();
    while(it.hasNext()){
    String s=(String)(it.next());
    o=(PrintWriter)(users.get(s));
    o.println(text);
    o.flush();
    }
    }
    else{
    o=(PrintWriter)(users.get(receiver));
    o.println(text);
    o.flush();
    }
    }
    }
      

  5.   

    java不清楚,.Net我倒是有研究,要用到server push ,Remoting等技术。看我的博客 http://www.cnblogs.com/nevermad