Simple Multiplayer game in Ruby on Rails?
I have created a simple card game in Ruby on Rails+ajax with jQuery for animation. It's working Ok, and I wanted to develop a multip开发者_JAVA百科layer version. It looks like it's not so simple as i thought. I have also tryed to port the game completely to JS into the Canvas tag. This is possible for me and I could make the animation there much better however I still don't know how to add a multiplayer part in it :( Does anyone tryed to use websockets with rails already? This is pretty fancy new technology. Is it worthy to stick with rails at all? I wish i could start from scratch in flash but I don't have any knowledge in it :( Any advise or tutorial will be helpful
Thanks
What's the simplest thing you can do to solve the user's problem? In this case it is probably "I want to be notified when the person I am playing against makes a move". Real-time isn't a requirement, just that within a second or two, the user is notified.
In which case it would be easiest to keep your Rails app as it is, and use Jquery to 'poll' for new 'moves' every few seconds using JQuery and a plugin such as http://plugins.jquery.com/project/smartupdater
I say this as someone who is currently building an application which uses Websockets in a major way. Whilst it is great in principle, we will be building a Flash version as a fallback for browsers which do not support it.
If you end up using websockets:
Websockets can be implemented with Rails, but not all hosting environments support it. Here are a couple links that might help you get started:
http://m.onkey.org/websockets-made-easy-with-cramp (requires Thin or Rainbows!)
http://www.pusherapp.com/ (if you are using Heroku, this one is a good option)
For the client-side, some javascript plugins have a flash fallback if the browser does not support websockets. Instead of implementing the entire game in flash, you might be better off using such a plugin. (Your server would still require websocket support.) Here is one option (requires jQuery):
https://github.com/ffdead/jquery-graceful-websocket
As you stated, websockets are very new, and as a result will be quite experimental. If all else fails, there are more common AJAX solutions out there. It is possible that for a simple card game, you could just periodically query the server using AJAX requests until the other player has made a move. This would probably increase server load, so I guess it depends on your needs.
Edit:
See also: Juggernaut
精彩评论