Monday, April 23, 2012

ruby: websocket server and websocket client can't work

I am a really newbie for this.but, I can't figure out what's wrong with this problem.



I just copied from somewhere online about a ruby websocket server and a ruby websocket client implementation. I had also installed ruby 1.93 on my windows xp. all looked fine but the websocket client doesn't really work well.



server side code:



equire 'em-websocket'

EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen { ws.send "Hello Client!"}
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "WebSocket closed" }
end


Client side code:



require 'eventmachine'
require 'em-http-request'

EventMachine.run {
http = EventMachine::HttpRequest.new("ws://localhost:8080").get :timeout => 0
http.errback { puts "oops" }
http.callback {
puts "WebSocket connected!"
http.send("Hello client")
}

http.stream { |msg|
puts "Recieved: #{msg}"
http.send "Pong: #{msg}"
}
}


the client side always spin out "oops" . that means there's is an error happened.



Could anybody give me any clue for this? I appreciate.





No comments:

Post a Comment