David T-L
2006-Feb-15 00:52 UTC
[Rails] Newbie Question: I want to open a socket on another server
Help! I need someone to get me in the right ballpark. I''ve been going around in circles for the last 24 hours. I want to open a TCPsocket on another server and eventually send and receive XML which is what the other servers speaks. I have both "Agile Web...with Ruby" and "Programming Ruby" but can find no examples. I''ve poured over the forum list, too. I''ve played with examples such as: <!-- Section that displays search box example --> <label for="search">Search term:</label> <%= text_field_tag :search %> <%= observe_field( :search, :frequency => 0.5, :update => :results, :url => { :action => :search }) %> <div id="results"></div> and I have changed this and that parameter and I know how and where to put the elements to WRITE some HTML, but I''m still clueless about getting some real work done, i.e., opening a socket, sending XML, receiving XML, sending, receiving, etc. Then I can write. I suspect I have to use these statements, but I''m lost regarding whether they go in views, models, layouts are they actions, or modules or helpers require "socket" STDOUT.flush s = TCPSocket.open("nnn.nnn.nnn.n", 9000) Just a little guidance or pointing me to an example and I bet I can work it out. Basically, I need to: 1. When a button is pushed [link_to_remote?? ] 2. Open a socket to another server (eventually I guess I''ll do this once ahead of time) Hmmm...I will do it ahead of time now (but where does the code go (this is really my major stumbling block)? Does it go in my index (but that''s all HTML, is it a helper that is called at the beginning of index?) 3. Send XML command that other server understands 4. Listen for response 5. Unwrap the response and print data on page 6. Once I have this figured out, I will substitute file contents for the returned data, but just getting the data will be a real victory. Thanks...again, I don''t expect anyone to write my code, just help me with a map of how one creates actions that are not HTML builders. Dave -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Feb-15 06:34 UTC
[Rails] Newbie Question: I want to open a socket on another server
David- I do something similar on one of my sites. I actually use standard http to fetch data from remote servers on the backend though, but you could easily use sockets instead. You should make a model class that doesn''t inherit from ActiveRecord and put the socket connection code in there. For example: class SockServ def self.transact(xml) # open a socket here and send the data in the xml var # read from the socket the response and use it as the return value for this method end end Then you can use this just like any other model. So say you have some xml to send to your socket server in params[:xml]. You can use it in your controller like so: def some_action @response = SockServ.transact(params[:xml]) end And thats all there is to it. You can just use it like any other data model by abstracting away the socket connection stuff. The key is to write your methods in the SockServ model with the self prefix so you can call them with SockServ.my_method syntax. Cheers- -Ezra On Feb 14, 2006, at 4:52 PM, David T-L wrote:> Help! > I need someone to get me in the right ballpark. I''ve been going > around > in circles for the last 24 hours. > > I want to open a TCPsocket on another server and eventually send and > receive XML which is what the other servers speaks. > > I have both "Agile Web...with Ruby" and "Programming Ruby" but can > find > no examples. I''ve poured over the forum list, too. > > I''ve played with examples such as: > <!-- Section that displays search box example --> > <label for="search">Search term:</label> > <%= text_field_tag :search %> > <%= observe_field( :search, > :frequency => 0.5, > :update => :results, > :url => { :action => :search }) %> > <div id="results"></div> > and I have changed this and that parameter and I know how and where to > put the elements to WRITE some HTML, but I''m still clueless about > getting some real work done, i.e., opening a socket, sending XML, > receiving XML, sending, receiving, etc. Then I can write. > > I suspect I have to use these statements, but I''m lost regarding > whether > they go in views, models, layouts are they actions, or modules or > helpers > > require "socket" > STDOUT.flush > s = TCPSocket.open("nnn.nnn.nnn.n", 9000) > > Just a little guidance or pointing me to an example and I bet I can > work > it out. > > Basically, I need to: > 1. When a button is pushed [link_to_remote?? ] > 2. Open a socket to another server (eventually I guess I''ll do this > once ahead of time) Hmmm...I will do it ahead of time now (but where > does the code go (this is really my major stumbling block)? Does > it go > in my index (but that''s all HTML, is it a helper that is called at the > beginning of index?) > > 3. Send XML command that other server understands > > 4. Listen for response > > 5. Unwrap the response and print data on page > > 6. Once I have this figured out, I will substitute file contents for > the returned data, but just getting the data will be a real victory. > > Thanks...again, I don''t expect anyone to write my code, just help me > with a map of how one creates actions that are not HTML builders. > > Dave > > > -- > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra@yakima-herald.com 509-577-7732