用Java构建一个irc客户端
我正在尝试用 Java 编写一个 ircBot 进行一些练习。我使用此示例代码作为基础。我正在尝试弄清楚如何让它从我的控制台读取文本,这样我就可以与机器人实际交谈。 有一个 while 循环,它从 ircserver 接收输入并将其输出到控制台并响应 PING。我假设我必须有另一个线程来获取用户的输入,然后使用相同的 BufferedWriter 再次将其吐出到 ircserver,但我无法弄清楚这一点。 任何帮助都会很棒!
I'm trying to write an ircBot in Java for some practice. I am using this sample code as the base. I'm trying to figure out how to get it to read in text from my console so I can actually talk to people with the bot.
There's the one while loop that takes in the input from the ircserver and spits it out to console and responds to PINGs. I'm assuming I have to have another thread that takes the input from the user and then uses the same BufferedWriter to spit it out to the ircserver again but I can't get that figured out.
Any help would be awesome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您链接到的代码中,“读取器”和“写入器”实例确实分别连接到您与 IRC 服务器建立的双向套接字的传入和传出端。
因此,为了从用户那里获取输入,您确实需要创建另一个线程,该线程以某种方式从用户那里获取命令并根据这些命令执行操作。最基本的模型自然是使用 System.in 来实现此目的,最好将其包装起来,以便您可以从用户检索整行输入,并将它们解析为命令。
要从 System.in 读取整行,您可以执行以下操作:
您还可以考虑使用适用于 Java 的 CLI 库之一,例如 JLine
In the code you have linked to, the 'reader' and 'writer' instances, are indeed connected to respectively the ingoing and outgoing ends of the two-way socket you have established with the IRC server.
So in order to get input from the User, you do indeed new another thread which takes commands from the user in some fashion and acts upon these. The most basic model, would naturally be to use System.in for this, preferably wrapping it so that you can retrieve whole line inputs from the User, and parse these as a command.
To read whole lines from System.in you could do something like this:
You could also consider using one of the CLI libraries that is out there for Java, like JLine
如果你真的想帮自己一个忙,我建议(在广泛使用它之后)切换到 pircbot。 Pircbot 确实是一个很棒的库,它可以让您在短短几分钟内启动并运行 IRC 机器人。查看网站上的一些示例,它非常易于使用。
If you really want to do yourself a favour, I recommend (after having used it extensively) switching to pircbot. Pircbot really is a wonderful library and will let you get an IRC bot up and running in just a few minutes. Check out some of the examples on the site, it's super easy to use.