Em-websocket gem with Ruby on Rails

I started developing a web socket game using em-websocket gem.

To test the application, I start the server by running

$> ruby ​​server.rb

and then I just open two browsers that go directly to the html file (without a web server) and start playing.

But now I want to add a web server, some database tables, other stones on Ruby on Rails.

How to achieve communication between my web socket server and my Ruby on Rails application? Should they work on the same server and run as a single process? Running on separate servers and exchanging data through AJAX?

I need to support authentication and other functions, such as updating the database at the end of the game, etc.

Thanks in advance.

+3
source share
3 answers
+3
source

Here is the deal. I also wanted to develop a websocket server client with a ruils-based ruby. However, ruby ​​rails are not very friendly with eventmachine. I struggled with having a websocket client, so I was able to copy / cut / paste from an existing library and eventually get the following two additional ones.

Em-Websocket Server

https://gist.github.com/ffaf2a8046b795d94ba0

ROR friendly web client client

https://gist.github.com/2416740

have the server code in the script directory, the beginning as in ruby ​​code.

    # Spawn a new process and run the rake command
    pid = Process.spawn("ruby", "web_socket_server.rb",
       "--loglevel=debug", "--logfile=#{Rails.root}/log/websocket.log",
        :chdir=>"#{Rails.root}/script") #, 
        :out => 'dev/null', :err => 'dev/null'
    Process.detach pid # Detach the spawned process

Then your client can be used as

ws = WebSocketClient.new("ws://127.0.0.1:8099/import")
Thread.new() do
  while data = ws.receive()
    if data =~ /cancel/
      ws.send("Cancelling..")
      exit
    end
  end
end
ws.close

I want a good friendly em-websocket client compatible with ROR, but not yet able to execute it.

/ , auth. rails. ( auth/db)

+3

I am working on a gem that might be useful with your current use case. The gem is called websocket-rails and was designed from the ground up to simplify the use of WebSockets inside a Rails application. Now it is in stable release.

Please let me know if you find this useful or consider where it might be missed.

+3
source

All Articles