0

I made a ReadMessage thread that should run in the background and read messages when they come in. This is how it looks:

 Thread ReadMessages = new Thread(new Runnable() {

        public void run() {

            try {

                while(socket.isConnected()){

                ServerMsg = in.readLine();
                jTextArea1.append(ServerMsg);

                }

            } catch (IOException ex) {
                System.out.println("Something went wrong! THREAD");
            }

        }

    });

I start it everytime i start my program:

public TwitchBotFenster() throws IOException, Exception {

    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    initComponents();

    ReadMessages.start();

    }

The problem I have, is that it only reads the first messages when I connect to twitch irc but if I write something in the chat it doesnt. I dont really get the problem. My thoughts when I made that were, whenever the socket is connected it should read all the messages.

EDIT:

This is how i send messages to the server:

public void SendMessage() throws IOException{

        try{

        if(BotName == null){
            this.jTextArea1.append("You are not logged in!\n");
        }else{
            this.ClientChatMessage = this.jTextField4.getText();
            out.write(":" + BotName + "!" + BotName + "@" + BotName + ".tmi.twitch.tv PRIVMSG #" + this.Channelname + " :"  + this.ClientChatMessage + "\n");
            out.flush();
            this.jTextField4.setText("");
            this.jTextArea1.append("*" + BotName + ">> " + ClientChatMessage + "\n");
        }

        this.jTextField4.setText("");

        }catch(Exception exe){
            System.out.println("Something went horribly wrong! (SendMessage/func)\n");
        } 

    } 

EDIT2:

Messages are sent whenever i press enter in my jTextField:

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                            

        try {
            SendMessage();
        } catch (IOException ex) {
            System.out.println("Something went horribly wrong! (SendMessage)\n");
        }

    }  
TomiG
  • 17
  • 5
  • Don't ever use `socket.isConnected()`! Next, please show the other side code as to how're you sending the message to this server? – Am_I_Helpful Nov 26 '16 at 12:16
  • edited the question. but why should i not use `socket.isConnected()` ? @Am_I_Helpful – TomiG Nov 26 '16 at 12:25
  • Is the `SendMessage()` operating in a separate thread? If not, then it should. Also, [why you should never rely on `Socket.isConnected()`](http://stackoverflow.com/questions/969866/java-detect-lost-connection/17889321#17889321)! – Am_I_Helpful Nov 26 '16 at 12:29
  • edited again, thank you for your answer. But when should I start the SendMessage and the ReadMessages threads? And how? @Am_I_Helpful – TomiG Nov 26 '16 at 12:37
  • Create separate Thread classes which would perform the sending and receiving of the messages on the client and the server, and call them separately from your UI code. Your first code seems OK, modify second code to allow the thread to send messages. – Am_I_Helpful Nov 26 '16 at 12:42
  • @Am_I_Helpful Thank you very much for helping me out! – TomiG Nov 26 '16 at 12:47

0 Answers0