Package chat.Network

Examples of chat.Network.ChatMessage


                                        name = name.trim();
                                        if (name.length() == 0) return;
                                        // Store the name on the connection.
                                        connection.name = name;
                                        // Send a "connected" message to everyone except the new client.
                                        ChatMessage chatMessage = new ChatMessage();
                                        chatMessage.text = name + " connected.";
                                        server.sendToAllExceptTCP(connection.getID(), chatMessage);
                                        // Send everyone a new list of connection names.
                                        updateNames();
                                        return;
                                }

                                if (object instanceof ChatMessage) {
                                        // Ignore the object if a client tries to chat before registering a name.
                                        if (connection.name == null) return;
                                        ChatMessage chatMessage = (ChatMessage)object;
                                        // Ignore the object if the chat message is invalid.
                                        String message = chatMessage.text;
                                        if (message == null) return;
                                        message = message.trim();
                                        if (message.length() == 0) return;
                                        // Prepend the connection's name and send to everyone.
                                        chatMessage.text = connection.name + ": " + message;
                                        server.sendToAllTCP(chatMessage);
                                        return;
                                }
                        }

                        public void disconnected (Connection c) {
                                ChatConnection connection = (ChatConnection)c;
                                if (connection.name != null) {
                                        // Announce to everyone that someone (with a registered name) has left.
                                        ChatMessage chatMessage = new ChatMessage();
                                        chatMessage.text = connection.name + " disconnected.";
                                        server.sendToAllTCP(chatMessage);
                                        updateNames();
                                }
                        }
View Full Code Here


                                        chatFrame.setNames(updateNames.names);
                                        return;
                                }

                                if (object instanceof ChatMessage) {
                                        ChatMessage chatMessage = (ChatMessage)object;
                                        chatFrame.addMessage(chatMessage.text);
                                        return;
                                }
                        }

                        public void disconnected (Connection connection) {
                                EventQueue.invokeLater(new Runnable() {
                                        public void run () {
                                                // Closing the frame calls the close listener which will stop the client's update thread.
                                                chatFrame.dispose();
                                        }
                                });
                        }
                });

                // Request the host from the user.
                String input = (String)JOptionPane.showInputDialog(null, "Host:", "Connect to chat server", JOptionPane.QUESTION_MESSAGE,
                        null, null, "localhost");
                if (input == null || input.trim().length() == 0) System.exit(1);
                final String host = input.trim();

                // Request the user's name.
                input = (String)JOptionPane.showInputDialog(null, "Name:", "Connect to chat server", JOptionPane.QUESTION_MESSAGE, null,
                        null, "Test");
                if (input == null || input.trim().length() == 0) System.exit(1);
                name = input.trim();

                // All the ugly Swing stuff is hidden in ChatFrame so it doesn't clutter the KryoNet example code.
                chatFrame = new ChatFrame(host);
                // This listener is called when the send button is clicked.
                chatFrame.setSendListener(new Runnable() {
                        public void run () {
                                ChatMessage chatMessage = new ChatMessage();
                                chatMessage.text = chatFrame.getSendText();
                                client.sendTCP(chatMessage);
                        }
                });
                // This listener is called when the chat window is closed.
View Full Code Here

TOP

Related Classes of chat.Network.ChatMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.