对于我的代码,我从客户端接收到以逗号分隔的数据流,每个字段具有不同的数据类型。收到后,我将它们拆分并写入一个数组。代码如下:
require 'socket'
s = TCPSocket.open('localhost',2000)
time = Time.now.to_i
s.puts(cf,PUB,#{time},001.41011,N,103.76822)
s.close
在 eventmachine 服务器上,我的代码如下:
require 'rubygems'
require 'eventmachine'
require './Load2MySQL.rb'
module EchoServer
def post_init
puts "-- someone connected to the echo server!"
end
def receive_data data
loc = Array.new(6)
loc = data.split(',').to_a
wr2db(loc[0], loc[1], loc[2], loc[3], loc[4], loc[5])
end
def unbind
puts "-- someone disconnected from the echo server!"
close_connection
# EventMachine::stop_event_loop
end
end
EventMachine::run {
EventMachine::start_server "0.0.0.0", 2000, EchoServer
}
我发现这样做,数组中的所有元素都将转换为字符串。我怎样才能保留他们的数据类型?
问候, New2RoR