从多个套接字读取(irc bot)
我试图制作一个连接到多个服务器的IRC bot,并且我无法一次从所有套接字读取。
我现在的代码:
#!/usr/bin/ruby
require 'socket'
servers = ["irc.chat4all.org"]
def connect(server, port, count)
puts "connecting to #{server}..."
@socket[count] = TCPSocket.open(server, port)
say("NICK link_hub", count)
say("USER link_hub 0 * link_hub", count)
read_data(count)
end
def say(msg, count)
@socket[count.to_i].puts msg.to_s
end
def say_channel(msg, count)
@socket[count.to_i].puts("PRIVMSG #test :"+msg.to_s)
end
def read_data(count)
until @socket[count].eof? do
msg = @socket[count].gets
puts msg
if msg.match(/^PING :(.*)$/)
say("PONG #{$~[1]}", count)
say("JOIN #test", count)
next
end
if msg.match(/`test/)
say_channel("connecting to efnet...", count)
Thread.new {
connect("irc.efnet.nl", 6667, count)
}
end
end
end
conn = []
count = 0
@socket = []
servers.each do |server|
connect(server, 6667, count)
count += 1
end
问题是,当我发送命令'test'时,它连接到efnet,但即使在线程中运行新连接,它也不会再读取其他套接字。 我只想同时读取两个套接字。 (变量'count'是套接字号)
谁能帮我吗? 非常感激!
你需要平等主义。
pids = []
servers.each do |server|
pids << fork do
connect(server, 6667, count)
count += 1
end
end
pids.each{|pid| Process.wait pid}
您可能想了解流程,线程和其他运营系统主题。
链接地址: http://www.djcxy.com/p/53461.html