problem with eventmachine

H

hemant

I have following code, the problem is, when i start a connection to
the socket through telnet, the data send by the server i get as
usual, but when i try to do the same through ruby code, the data send
by the server won't come to the client..until the server is shutdown.


def receive_data data
data = data.chomp.strip
if is_symbol?(data)
add_symbol(data)
local_connection = self
start_send_thread(local_connection)
end
end

def start_send_thread(local_connection)
unless @send_started
p "Trying to send the data"
@send_started = true
@local_thread = Thread.new do
loop do
broadcast_data(local_connection)
sleep(0.1)
p "I am running"
end
end
end
end

def broadcast_data(local_connection)
for x in @local_symbols
local_connection.send_data "#{x}"+rand(10).to_s
end
end

def unbind
p "Client closed the connection"
Thread.kill(@local_thread)
end
 
H

hemant

I have following code, the problem is, when i start a connection to
the socket through telnet, the data send by the server i get as
usual, but when i try to do the same through ruby code, the data send
by the server won't come to the client..until the server is shutdown.

Ok that was one stupid wtf....
i forgot to put "\n" in the end of the string, i guess.
 
F

Francis Cianfrocca

I have following code, the problem is, when i start a connection to
the socket through telnet, the data send by the server i get as
usual, but when i try to do the same through ruby code, the data send
by the server won't come to the client..until the server is shutdown.

Two points:
First, if you're connecting to this program using telnet, the behavior
will depend on the platform that you run telnet on. The telnet client
on Windows sends data byte-by-byte as you type it in. On Linux, Mac or
other Unix, telnet buffers the data line by line.

Second, and extremely important: you do *not* need to spin a thread to
send your data. The whole point of EventMachine is that it allows you
to handle many connections at once, without using threads. Threads
slow your program down and make it harder to debug.

If you really want to hammer your telnet client with random data based
on @local_symbols, use EventMachine#add_periodic_timer rather than
spinning a thread.
 
H

hemant

Two points:
First, if you're connecting to this program using telnet, the behavior
will depend on the platform that you run telnet on. The telnet client
on Windows sends data byte-by-byte as you type it in. On Linux, Mac or
other Unix, telnet buffers the data line by line.

Second, and extremely important: you do *not* need to spin a thread to
send your data. The whole point of EventMachine is that it allows you
to handle many connections at once, without using threads. Threads
slow your program down and make it harder to debug.

If you really want to hammer your telnet client with random data based
on @local_symbols, use EventMachine#add_periodic_timer rather than
spinning a thread.

Thanks for replying.
Random values were just the placeholders, the actual data would be
more important i guess. But my understanding of add_periodic_timer
says that, it would be global to entire program. But what i want is
basically, a method which would be client specific. By being client
specific, I mean...each connection, should have a callback, which
would start sending the data. Please correct me, If i am wrong.

Basically what i want is...push the data to the clients
asynchronously. Using recieve_data I can change @local_symbols and
hence the data pushed to the client should change accordingly.

So, How do i do this?
 
F

Francis Cianfrocca

Sorry if I missed you, Hemant- I was hoping to get a bit more
information on your home-grown data-vending server. If the protocol is
appropriate for EM, then you can implement your push server without
spinning any threads.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top