I am creating a chat client/server system in Ruby.
My server will be hosted on a laptop or something (this is a class project, so not much processing power will be needed) and then I plan for the client to take place in a web browser.
I will feed it the HTML for two textboxes: one in which the user can type and the other will display the chat history.
My problem is that while I can easily feed the HTML code to the browser and get it to display the chat开发者_开发知识库 (navigate to the ip address:port) I can't figure out how I can return what is input in the textbox to the server.
Does anybody know how I could do this?
I'd suggest using a lightweight framework like Sinatra to handle this. It's simple enough to get things done quickly without a lot of required reading, but powerful enough to expand your chat application significantly, should you want.
The downside of using a web-based client is that the chat log will only be refreshed on the client after they ask the server for the newest information; namely, at each page refresh, instead of in real time.
You can get around this with some slick Javascript (mostly XMLHTTPRequest) to ask for new content at a regular interval, like how Stack Overflow shows you when new answers have been posted as you're typing an answer of your own.
It sounds like you need a basic knowledge of how CGIs work. Once you know that you will find it easier to work with Sinatra, as @echoback recommended, or Padrino, Rails, or work with other languages.
This is a pretty basic CGI. It generates a simple form, along the lines of what you were talking about, then walks through the environment table passed to Ruby by the web server, sorts by the keys, and outputs a table in sorted order. Most of the fields directly apply to either the web server itself, or to the CGI, such as the query sent by the browser, along with its headers sent to the server saying what its capabilities are:
#!/usr/bin/env ruby
puts "Content-Type: text/html"
puts
puts "<html><head><style type='text/css'>body{font-family: monospace;}</style></head><body>"
puts "<form name='foo' action='test_cgi.rb'>"
puts "<input type='textinput' name='inputbox'></input><br />"
puts "<textarea name='textareabox'></textarea><br />"
puts "<input type='submit'></input>"
puts "</form>"
puts "<h4>ENVIRONMENT:</h4>"
puts "<table>"
ENV.keys.sort.each do |k|
puts "<tr><td>#{k}</td><td>#{ENV[k]}</td></tr>"
end
puts "</table>"
puts "</body></html>"
Copy that code, store it into a Ruby file called test_cgi.rb
, then set the executable bit on the file. Move that file into the cgi-bin
directory of your web server on your machine. Use your browser to access the file (http://localhost:8080/cgi-bin/test_cgi.rb
or something similar), and watch the output in the table change as you enter different values in the form and submit them.
Once you understand that round-trip from server to browser to server you'll be in a good place to learn how Sinatra builds on Rack to supply more features, more easily, than doing it all yourself with a CGI.
精彩评论