【发布时间】:2013-08-17 22:56:49
【问题描述】:
我正在尝试使用 WebSockets 编写 IRC 客户端。我在 GitHub 上找到的 IRC 客户端使用 EventMachine,但我也尝试使用 WebSockets 在连接时通知任何连接的客户端。不过,我觉得我不是很了解 EventMachine,因为虽然客户端成功连接并加入了 IRC 频道,但puts 'Connected...' 和后续行都被执行了。
我认为这是因为我对 EventMachine 存在根本性的误解。
EM.run {
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |websocket|
websocket.onopen {
irc = Net::YAIL.new(
:address => 'irc.my-example-server.net',
:port => 6667,
:username => 'MyExample',
:realname => 'My Example Bot',
:nicknames => ['MyExample1', 'MyExample2', 'MyExample3']
)
irc.on_welcome proc { |event|
irc.join('#ExampleChannel')
EM.next_tick {
puts 'Connected...'
websocket.send({ :message => 'Connected' })
}
}
irc.start_listening!
}
end
}
【问题讨论】:
标签: ruby websocket eventmachine