views:

16

answers:

2

I want to send a hash populated with data from a EventMachine client to a server. The problem is that the server receive_date method just prints a string.

The server:

  def receive_data(data)
    send_data("Ready")
    puts data[:total]
  end

The client:

def receive_data(data)
  send_data( get_memory() )
end

def get_memory
  sigar = Sigar.new
  mem = sigar.mem
  swap = sigar.swap
  memory = { :total => (mem.total / 1024), :cat => "kwiki", :mouse => "squeaky" }
end

This line: puts data[:total]

prints only nil

+1  A: 

Why don't you convert the Hash to a JSON string before sending it, and convert it back to a Hash when the server receive it?

remi
I was thinking about JSON, but I was wondering if EventMachine was a little bit more magical. Than you.
rtacconi
+1  A: 

You need to serialize the data that you send over the wire. In other words, normally everything is outputted as plain ascii. In your case you could use YAML for serialization, since you memory hash only contains ascii chars.

client:

require 'yaml'

def receive_data(data)
  send_data(get_memory().to_yaml)
end

server:

require 'yaml'

def receive_data(data)
  puts YAML.load(data)
end

Of course there are other serialization methods like JSON, or something.

Wouter de Bie