单个 EventMachine 反应器中的多个服务器 [英] Multiple servers in a single EventMachine reactor
问题描述
是否可以在单个事件机器中运行多个服务器?
Is it possible to run multiple servers within a single event machine?
我的意思是单个客户端连接可以同时使用多个服务.例如,登录服务器对用户进行身份验证,然后用户可以同时使用聊天室和简单的游戏,例如带有单个客户端套接字的跳棋?
What I mean is that multiple services can be used concurrently by a single client connection. For example, a login server authenticates a user and then a user can use both a chat room and a simple game such as checkers with a single client socket concurrently?
还是每个服务都需要多个 eventmachine 反应器?
Or do I need multiple eventmachine reactors for each service?
推荐答案
我试过了,它正在工作:
I tried this and it's working:
#!/usr/bin/env ruby
require 'eventmachine'
module EchoServer
def post_init
puts "-- someone connected to the echo server!"
end
def receive_data data
send_data ">>>you sent: #{data}"
close_connection if data =~ /quit/i
end
def unbind
puts "-- someone disconnected from the echo server!"
end
end
EventMachine::run {
EventMachine::start_server "127.0.0.1", 8081, EchoServer
EventMachine::start_server "127.0.0.1", 8082, EchoServer
EventMachine::start_server "127.0.0.1", 8083, EchoServer
}
这里有 3 个不同端口的 echo 服务.(我懒得去实现不同的服务了.)
Here you get 3 echo services with different ports. (I was too lazy to implement different services.)
因此,构建一个巨大的多服务包装器非常容易.
So, it's very easy to build a huge multi service wrapper.
更新
基于条件启动 EM 服务器的简单代码示例:
Simple code example for condition based start of a EM server:
#!/usr/bin/env ruby
# encoding: utf-8
require 'eventmachine'
module EchoServer
def post_init
puts "-- someone connected to the echo server!"
end
def receive_data data
send_data ">>>you sent: #{data}"
close_connection if data =~ /quit/i
end
def unbind
puts "-- someone disconnected from the echo server!"
end
end
$check_ok = false
EventMachine::run {
puts "checker var is: #{$check_ok}"
EventMachine::start_server "127.0.0.1", 8081, EchoServer
EventMachine::start_server "127.0.0.1", 8082, EchoServer
puts "echos on 8081 and 8082 started."
# periodic timer check - every 1 sec
EventMachine.add_periodic_timer(1) {
if $check_ok
EventMachine::start_server "127.0.0.1", 8083, EchoServer
$check_ok = false
puts "echo on 8083 started!"
end
}
# timer triggered after 10 secs - only once!
EventMachine.add_timer(10) {
$check_ok = true
puts "checker var is #{$check_ok} now!"
}
}
在此示例中,端口 8083 上的回显服务器在应用启动后约 10 秒启动.尝试在这个定时器前后telnet localhost 8083
,你会看到效果.
In this example the echo server on port 8083 is started ~10 secs after app start. Try to telnet localhost 8083
before and after this timer, you'll see the effect.
对于每 1/100 秒的检查,您还可以使用小于 1 秒的值,例如 0.01.
You also can use values lower than 1 sec like 0.01 for every 1/100th sec checks.
这可能是您提出自己想法的起点.定期计时器是您的内部循环,您可以在其中连接条件检查以启动更多服务.
This might be your starting point for your own ideas. The periodic timer is your internal loop, where you hook in your conditional checks for starting further services.
优秀教程 (PDF):eventmachine 介绍(博文)
Good tutorial (PDF): eventmachine introduction (blog post)
这篇关于单个 EventMachine 反应器中的多个服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!