我有一个Pinoccio 微控制器(非常棒,试试吧)。微控制器为其服务器打开一个套接字。我正在一个 Ruby 应用程序中编写 TCP Socket 服务器,我将在其中使用Celluloid::IO。作为我的向导,我在 Node 中遵循这个实现,称为pinoccio-server
我写了一些测试代码,尝试与 Pinoccio 微控制器通信。我可以毫无问题地读取它,但是当我将数据写回套接字时,我永远不会得到我期望的行为。这是代码,有人可以告诉我我是否在滥用 Celluloid::IO 或套接字?
我有一个Pinoccio 微控制器(非常棒,试试吧)。微控制器为其服务器打开一个套接字。我正在一个 Ruby 应用程序中编写 TCP Socket 服务器,我将在其中使用Celluloid::IO。作为我的向导,我在 Node 中遵循这个实现,称为pinoccio-server
我写了一些测试代码,尝试与 Pinoccio 微控制器通信。我可以毫无问题地读取它,但是当我将数据写回套接字时,我永远不会得到我期望的行为。这是代码,有人可以告诉我我是否在滥用 Celluloid::IO 或套接字?
当时如果这个问题,语法是正确的。但现在不是,截至0.17.0
... Celluloid
,但这不是答案,这只是一个序言。我已经分叉并编辑了您的要点:
https://gist.github.com/digitalextremist/7dc74b03587cd4b3b7dd
require 'celluloid/current'
require 'celluloid/io'
require 'json'
class TestServer
include Celluloid::IO
finalizer :shutdown
def initialize(host, port)
puts "*** Starting echo server on #{host}:#{port}"
# Since we included Celluloid::IO, we're actually making a
# Celluloid::IO::TCPServer here
@server = TCPServer.new(host, port)
end
def shutdown
@server.close rescue nil
end
def run
loop {
async.handle_connection(@server.accept)
}
end
def handle_connection(socket)
addr = *socket.peeraddr
puts "*** Received connection from #{addr[3]}:#{addr[1]}"
# This is just a test, followed this format:
# https://github.com/soldair/pinoccio-server/blob/master/troop.js#L163
cmd = {
:type => "command",
:to => 1,
:timeout => 10000,
:command => "led.on"
}
json_cmd = "#{cmd.to_json}\n"
socket.write json_cmd # The light never turns on. Why?
puts json_cmd
loop {
puts socket.readpartial(4096)
}
rescue EOFError
puts "*** #{addr[3]}:#{addr[1]} disconnected"
rescue => ex
echo "Trouble with socket: [#{ex.class}] #{ex.message}"
ensure
socket.close rescue nil
end
end
TestServer.supervise(as: :test_server, args: ["0.0.0.0", 1234])
#de Not needed. Killed by Celluloid.shutdown already...
#de trap("INT") { supervisor.terminate; exit }
#de Avoid starting the server in the constructor, then sleeping.
#de Could end up with a race condition, and/or botch instantiation.
#de But with that being said, you need to detect crashes... so:
loop {
begin
Celluloid[:test_server].run
rescue => ex
echo "Trouble with supervised server: [#{ex.class}] #{ex.message}"
echo "Waiting 1.26 seconds for actor to be reinstantiated."
sleep 1.26
end
}
Celluloid
,让它正常启动。initialize
。Celluloid
自行启动。你最初开始的代码应该可以工作,或者至少它应该给你一个明确的理由为什么它不工作。上面的代码现在与新版本兼容Celluloid
,0.17.0
并且与新版本相同Celluloid::IO
......如果您有任何进一步的问题,请包括具体错误。
注意:您可能需要检测到服务器崩溃并且仍然保持打开您期望可用的端口,并且示例中未包含该端口......但Celluloid::IO
测试套件中给出了一个示例: