コード例 #1
0
ファイル: ChatNode.java プロジェクト: gmyrianthous/pilotta
  @Override
  public void run() {
    try {
      while (true) {
        clientMessage = this.nodeInput.readUTF();
        if (clientMessage.equals("chatClient-hello")) {
          this.nodeOutput.writeUTF("chat-request_name_id");

          this.nodeName = nodeInput.readUTF();
          this.nodeID = nodeInput.readInt();

          System.out.println("ChatClient: " + this.nodeName + " with ID: " + this.nodeID);
        } else if (clientMessage.equals("chatClient-sending_message")) {
          nodes = ChatServer.getNodes();
          messageToBeSent = nodeInput.readUTF();
          for (int i = 0; i < 4; i++) {
            this.nodes.get(i).nodeOutput.writeUTF("chat-sending_message");
            this.nodes.get(i).nodeOutput.writeUTF(this.nodeName + " says: " + messageToBeSent);
          }
          System.out.println("Message has been sent: " + messageToBeSent);
        }
      } // while
    } catch (IOException ex) {
      System.err.println("Error in ChatNode");
      ex.printStackTrace();
    }
  } // run()