开发者

Refresh userlist in all frames

开发者 https://www.devze.com 2023-02-24 02:01 出处:网络
I try to make a chat. My question is how can i refresh the userlist when another user had entered so that the user list can be visible in all opened frames?

I try to make a chat. My question is how can i refresh the userlist when another user had entered so that the user list can be visible in all opened frames?

import java.io.*;

public class MulticastChat implements Runnable, WindowListener, ActionListener {
    public InetAddress groupp;
    public int port;

    public MulticastChat (InetAddress group, int port) throws IOException {
    this.groupp = group;
    this.port = port;
    start();
    }
    public String [] user;
    public Frame frame;
    public TextArea output;
    public TextField input;
    public Button button;
    public Panel panel;
    public Panel p;
    public List list=new List(8);
    public TextField tf;

    public Thread listener;



    protected MulticastSocket socket;
    protected DatagramPacket outgoing, incoming;

    protected void initNet () throws IOException {
    socket = new MulticastSocket (port);
    socket.setTimeToLive (5);
    socket.joinGroup (groupp);
    outgoing = new DatagramPacket (new byte[1], 1, groupp, port);
    incoming = new DatagramPacket (new byte[65508], 65508);

    }
    public synchron开发者_如何转开发ized void start () throws IOException {
    if (listener == null) {
        initAWT ();
        initNet ();
        listener = new Thread (this);
        listener.start ();
        frame.setVisible (true);

        list.add(frame.getTitle());
    }
    }

    public void initAWT () {
    frame = new Frame
    ();
    frame.addWindowListener (this);

    tf=new TextField(10);
    output = new TextArea ();
    output.setEditable (false);
    button=new Button("Login");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("Login")) {
            try {
            if(!tf.getText().isEmpty()){

                byte[] utf = (tf.getText()+" Joined").getBytes ("UTF8");
                frame.setTitle(tf.getText());
                outgoing.setData (utf);
                outgoing.setLength (utf.length);
                socket.send (outgoing);
                input.setText ("");
                list.addItem(tf.getText());
                tf.setVisible(false);
                button.setVisible(false);
            }else{
                JOptionPane.showMessageDialog(frame, "Enter login name");
            } 
            } catch (IOException ex) {
                handleIOException (ex);
            }
        }
        }
    });
    panel=new Panel(new BorderLayout());
    p=new Panel(new BorderLayout());
    input = new TextField ();
    input.addActionListener (this);
    frame.setLayout (new BorderLayout ());
    panel.add(tf,"South");
    panel.add(list,"North");
    panel.add(button,"Center");
    frame.add(panel,"East");
    p.add (output, "Center");
    p.add (input, "South");
    frame.add(p,"West");
    frame.pack ();

    }


    public synchronized void stop () throws IOException {
    String s=frame.getTitle();
    frame.dispose();

    byte[] utf = (s+"Leaved").getBytes ("UTF8");
        outgoing.setData (utf);
        outgoing.setLength (utf.length);
        socket.send (outgoing);
    if (listener != null) {
        listener.interrupt ();
        listener = null;
        try {
        socket.leaveGroup (groupp);
        } finally {
        socket.close ();
        }
    }
    }

    public void windowOpened (WindowEvent event) {
    input.requestFocus ();
    }

    public void windowClosing (WindowEvent event) {
    try {

        stop ();

    } catch (IOException ex) {
        ex.printStackTrace ();
    }
    }

    public void windowClosed (WindowEvent event) {
    }
    public void windowIconified (WindowEvent event) {}
    public void windowDeiconified (WindowEvent event) {}
    public void windowActivated (WindowEvent event) {}
    public void windowDeactivated (WindowEvent event) {}

    public void actionPerformed (ActionEvent event) {
    try {
        byte[] utf = event.getActionCommand ().getBytes ("UTF8");
        outgoing.setData (utf);
        outgoing.setLength (utf.length);
        socket.send (outgoing);
        input.setText ("");


    } catch (IOException ex) {
        handleIOException (ex);
    }
    }

    protected synchronized void handleIOException (IOException ex) {
    if (listener != null) {
        output.append (ex + "\n");
        input.setVisible (false);
        frame.validate ();
        if (listener != Thread.currentThread ())
        listener.interrupt ();
        listener = null;
        try {
        socket.leaveGroup (groupp);
        } catch (IOException ignored) {
        }
        socket.close ();
    }
    }

    public void run () {

    try {

        while (!Thread.interrupted ()) {
        incoming.setLength (incoming.getData ().length);
        socket.receive (incoming);
        String message = new String
        (incoming.getData (), 0, incoming.getLength (), "UTF8");
        output.append (incoming.getAddress().toString()+frame.getTitle()+message + "\n");
        }
    } catch (IOException ex) {
        handleIOException (ex);
    }

    }
    public void refresh(){

    }

    public static void main (String[] args) throws IOException {

    InetAddress group;

        group = InetAddress.getByName("235.235.235.235");

    int port = 2999;
    new Thread(new MulticastChat(group, port));
    }
}


  • Update the list at specific time interval.(polling mechanism)
  • Send some data (hash) from client and check it on server if there is update needed return packet with some instruction so that client will refresh the list


I know this answer is not so much descriptive, but try to make it creating an event handler, just to control when a new user comes into the chat. Just something like:

public void newUserIn (UserEvent event) {}

or so...


First, I would suggest you separate out your code into a number of classes, to make it easier to read and maintain. Have a class for your network IO, and another for the Window stuff. Link the two with listener interfaces.

Secondly, you need to create a multicast message that indicates a user has joined. This might be something like "[NEW-USER:xxxxxx]".

Then you need to examine the incoming messages in your client to identify the 'new user' message. You can then display that user in your user interface.

You should also prevent users from spoofing your new user message. If they type in something that matches the new user pattern, escape it so that it doesn't get misinterpreted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号