Reputation: 1399
Using eventmachine gem i am trying to send and receive data on localhost. Following is the code of my client and server files.
server.rb
class BCCServer < EM::Connection
attr_accessor :server_socket
def post_init
puts "BCC Server"
end
def recieve_data(data)
puts "Received data: #{data}"
send_data "You sent: #{data}"
end
end
EM.run do
EM.start_server("0.0.0.0", 3000, BCCServer)
end
client.rb
class DCClient < EventMachine::Connection
def post_init
puts "Sending "
send_data "send data"
close_connection_after_writing
end
def receive_data(data)
puts "Received #{data.length} bytes"
end
def unbind
puts 'Connection Lost !'
end
end
EventMachine.run do
EventMachine::connect("127.0.0.1", 3000, DCClient)
end
I executed both server and client files in separate console. Following is the output of client
Client output
Sending
Connection Lost !
Server output
BCC Server
............>>>10
In the server file i have printed the data received but its showing "............>>>10". Where i am doing the mistake?
Thanks
Upvotes: 0
Views: 338
Reputation: 86
if you look at the EM::Connection implementation
https://github.com/eventmachine/eventmachine/blob/master/lib/em/connection.rb
def receive_data data
puts "............>>>#{data.length}"
end
Method receive_data returns exactly what you are experiencing. That means original method gets called and not yours. That means one thing. You have a typo in a method which you tried to override :)
In BCCServer you have
recieve_data(data)
instead of
receive_data(data)
Upvotes: 3