user1346968
user1346968

Reputation: 31

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.

Upvotes: 1

Views: 995

Answers (1)

forforf
forforf

Reputation: 915

Actually, digging a bit deeper your code is fine, the problem is something changed in the dependencies of the latest version. Steps to fix:

Uninstall the current gems

 gem uninstall em-http-request --version="1.0.2"  
 gem uninstall em-socksify --version "=0.2.0"     1.0.0.beta.4
 gem uninstall eventmachine --version "=1.0.0.beta.4"

Then install these gems

 gem install em-http-request --version "=0.3"
 gem install em-socksify --version "=0.1"
 gem install eventmachine --version "0.12.10"

And things should work. If this is for some type of serious project, rather than learning, you'll want to investigate what changes are breaking the websocket client-server.

Note: This worked for me with Ruby 1.9.2 on Ubuntu, it should work for you, but then again it might not.

Upvotes: 2

Related Questions