I
I
IvanN7772016-04-18 21:37:08
ruby
IvanN777, 2016-04-18 21:37:08

Can't understand the ruby ​​websocket test case, although I have a guess, can you help?

<html>
  <head>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
    <script>
      $(document).ready(function(){
        function debug(str){ $("#debug").append("<p>"+str+"</p>"); };

        ws = new WebSocket("ws://yourservice.com/websocket");
        ws.onmessage = function(evt) { $("#msg").append("<p>"+evt.data+"</p>"); };
        ws.onclose = function() { debug("socket closed"); };
        ws.onopen = function() {
          debug("connected...");
          ws.send("hello server");
        };
      });
    </script>
  </head>
  <body>
    <div id="debug"></div>
    <div id="msg"></div>
  </body>
</html>

require '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

https://www.igvita.com/2009/12/22/ruby-websockets-...
This is where I started reading about web socket connections.
There is a simple code on the client in the browser, there is a handler and everything is clear with this.
The subtlety of the details is not entirely clear.
Since it's just ruby ​​(not rails) we set the 'em-websocket' gem globally.
Connect thin also via require? and run on the command line like this in the ruby ​​directory of the sudo thin start -p 8080 file.
And here ws://yourservice.com/websocket is the address of the virtual host?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Dyachuk, 2016-04-28
@IvanN777

Well, first of all, thin is single-threaded, use puma.
In general, you don't need an em. em serves one function here - support for multiple connections.
I don't know what kind of virtual host ws - protocol, yourservice.com url, websocket - path. all this can be on ws://0.0.0.0/subscribe
And by the way, rails has edgeapi.rubyonrails.org/classes/ActionController/L...
SSE browsers have been able to for a long time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question