2

我的朋友不久前让我写一个插件,最近(大约五分钟前)他让我添加一个功能,人们可以登录到 esper.net 上的特定 IRC 并在上面聊天。一切都设置好了 - 我有一个命令让他们登录,它会让他们使用他们在 MineCraft 中的用户名的昵称加入。服务器使用聊天机器人登录用户,以便他们可以聊天,我已经设置好所有内容,以便当输入命令 (/irc login ) 时,他们会向机器人发送正确的消息(具体来说,它看起来像是他们写的/msg 在 IRC 聊天中识别)。好吧,我让插件设置了一个所有内容都已格式化的字符串,但我需要知道如何实际将消息发送到 IRC。下面是我的代码(当用户输入命令“/irc”和“player”时,整个事情都会触发 是玩家输入任何命令时设置的对象)。我还想要一些代码来接收机器人发回的内容,这样我就知道玩家是否成功登录。

if(args[0].equalsIgnoreCase("login") && args.length == 3) {
    String msg = "/msg <bot name> login " + args[1] + " " + args[2];
    //args[1] is the username and args[2] is the password
    String userName = args[1];
    //Creating the connection with a nickname of userName
    //Here is where I need to send that message through IRC
}
else {
    String msg;
    for(int i = 0; i <= args.length; i++) {
        msg += args[i] + " ";
    }
}

顺便说一句,我还不知道这个机器人的名字,因为我的朋友还在为它写代码。我想当我得到它时我可以把它放进去。此外,域名仍有待确定,因为他需要购买网站并进行设置。任何使此代码功能更快的建议也很棒。

4

2 回答 2

1

我认为您应该研究 Java 套接字连接

但这是一个基本的 IRC 连接

class IRC
{
      //Connection Details
      String server = "IRC ADDRESS";
      String nick = "NICKNAME";
      String login = "LOGIN";
      String Pass = "PASS";  
      String channel = "CHANNLE";

  //Socket Stuff
  Socket socket;
  BufferedWriter writer;
  BufferedReader reader;


      public void HandleChat() throws Exception 
      {
           socket = new Socket(server, 6667);

           writer = new BufferedWriter(
               new OutputStreamWriter(socket.getOutputStream( )));

           reader = new BufferedReader(
               new InputStreamReader(socket.getInputStream( )));


           // Log on to the server.      
           writer.write("PASS " + Pass + "\r\n");
           writer.write("NICK " + nick + "\r\n");
           writer.write("USER " + login +"\r\n");
           writer.flush( );

           // Read lines from the server until it tells us we have connected.
           String line = null;

           while (((line = reader.readLine( )) != null))
           {
           }            
      }
}
于 2013-04-01T13:49:17.807 回答
1

这是一些可以连接到服务器的类。您可以使用它并根据需要对其进行修改。您需要做的就是创建一个新的 ChatClient 实例并为其提供正确的参数,然后它将与服务器连接。

聊天客户端

    package com.weebly.foxgenesis.src;
import java.net.*;
import java.io.*;

public final class ChatClient
{  
    private Socket socket = null;
    private DataOutputStream streamOut = null;
    private ChatClientThread client = null;
    private String serverName = "localhost";
    private int serverPort = -1;
    private final ChatReciever output;

/**
 * Create a new ChatClient that connects to a given server and receives UTF-8 encoded messages
 * @param a Client class
 */
public ChatClient(ChatReciever chatReciever)
{ 
    output = chatReciever;
    serverPort = chatReciever.getPort();
    serverName = chatReciever.getHost();
    connect(serverName, serverPort);
}

private void connect(String serverName, int serverPort)
{  
    output.handleLog("Establishing connection. Please wait ...");
    try
    {  
        socket = new Socket(serverName, serverPort);
        output.handle("Connected to chat server");
        open();
    }
    catch(UnknownHostException uhe)
    {  
        output.handleError("Host unknown: " + uhe.getMessage()); 
    }
    catch(IOException ioe)
    {  
        output.handleError("Unexpected exception: " + ioe.getMessage()); 
    } 
}

/**
 * Sends a message to the server through bytes in UTF-8 encoding 
 * @param msg message to send to the server
 */
public void send(String msg) throws IOException
{  
    streamOut.writeUTF(msg); 
    streamOut.flush();
}

/**
 * forces the ChatReciever to listen to a message (NOTE: this does not send anything to the server)
 * @param msg message to send
 */
public void handle(String msg)
{  
    output.handle(msg);
}

private void open() throws IOException
{   
    streamOut = new DataOutputStream(socket.getOutputStream());
    client = new ChatClientThread(this, socket); 
}

/**
 * tries to close 
 */
public void close() throws IOException
{  
    if (streamOut != null)  
        streamOut.close();
    if (socket    != null)  
        socket.close(); 
}

/**
 * closes the client connection
 */
@SuppressWarnings("deprecation")
public void stop()
{
    if(client != null)
        client.stop();
    client = null;
}

/**
 * checks if the ChatClient is currently connected to the server
 * @return Boolean is connected
 */
public boolean isConnected()
{
    return client == null ?(false) : (true);
}
}

聊天服务器线程

 package com.weebly.foxgenesis.src;
import java.net.*;
import java.io.*;

public final class ChatClientThread extends Thread
{  
    private Socket           socket   = null;
    private ChatClient       client   = null;
    private DataInputStream  streamIn = null;

    public ChatClientThread(ChatClient _client, Socket _socket)
    {  
        client   = _client;
        socket   = _socket;
        open();  
        start();
    }
    public void open()
    {  
        try
        {  
            streamIn  = new DataInputStream(socket.getInputStream());
        }
        catch(IOException ioe)
        {  
            System.out.println("Error getting input stream: " + ioe);
            client.stop();
        }
    }
    public void close()
    {  
        try
        {  
            if (streamIn != null) streamIn.close();
        }
        catch(IOException ioe)
        {  
            System.out.println("Error closing input stream: " + ioe);
        }
    }
    public void run()
    {  
        while (true)
        {  
            try
            {  
                client.handle(streamIn.readUTF());
            }
            catch(IOException ioe)
            { 
                System.out.println("Listening error: " + ioe.getMessage());
                client.stop();
            }
        }
    }
}

聊天接收者

package com.weebly.foxgenesis.src;

public interface ChatReciever 
{
    /**
     * gets the IP address of the host
     * @return String IP address
     */
    public String getHost();

    /**
     * gets the port of the host
     * @return Integer port of host
     */
    public int getPort();

    /**
     * sends a message from the server to the implementing class
     * @param msg message from the server
     */
    public void handle(String msg);

    /**
     * sends an error to the implementing class
     * @param errorMsg error message
     */
    public void handleError(String errorMsg);

    /**
     * Sends a message to the log
     * @param msg message to send
     */
    public void handleLog(Object msg);
}
于 2014-01-03T09:36:03.603 回答