Sinatra 将数据从 TCP 传递到 websockets

Sinatra pass data from TCP to websockets

我正在尝试使用 Sinatra 来侦听自定义 HTTP 方法。无论从那里接收到什么数据,都会通过 websocket 实时传递给 n number 个客户端。

这是进度 -

  1. 我已经能够让 Sinatra 监听自定义 HTTP 方法 NOTIFY。
  2. 我也有一个 websocket 服务器 运行。
  3. 但我无法弄清楚最后一部分,即在接收到数据时将来自 NOTIFY 的数据传递给 websocket 客户端。如何做到这一点?

Ruby app.rb

require 'sinatra'
require 'sinatra-websocket'

configure do
  class << Sinatra::Base
    def notify(path, opts={}, &block)
      route 'NOTIFY', path, opts, &block
    end
  end
  Sinatra::Delegator.delegate :notify
end

set :server, 'thin'
set :sockets, []
set :bind, '0.0.0.0'
enable :sessions # For some reason sessions don't work. Relying on class variable.



notify '/' do   
    @json = request.body.read    
    session[:order] = @json
    @@pass = session[:order] # Saved in class variable. Hacky?
    puts "NOTIFY"
    puts "is session[:order] nil? #{session[:order].nil?}" # returns false
    puts "END NOTIFY"
end    

get '/' do
  if !request.websocket?
    puts @@pass # prints NOTIFY data successfully

    puts "GETS"
    puts "is session[:order] nil? #{session[:order].nil?}" # returns true. Session dont work
    puts "END GETS"

    @new = @@pass
    erb :index
  else
    request.websocket do |ws|
      ws.onopen do
        ws.send("Hello World!")            
        settings.sockets << ws
      end
      ws.onmessage do |msg|
        EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
        ws.send(@@pass) # this will send on each message. Not real-time
      end
      ws.onclose do
        warn("websocket closed")
        settings.sockets.delete(ws)
      end
    end
  end
end    

__END__

在线 ERB 索引

@@ index
<html>
  <body>
     <h1>Simple Echo &amp; Chat Server</h1>
     <p><%= @new %></p>
     <form id="form">
       <input type="text" id="input" value="send a message"></input>
     </form>
     <div id="msgs"></div>


  <script type="text/javascript">
    window.onload = function(){
      (function(){
        var show = function(el){
          return function(msg){ el.innerHTML = msg + '<br />' + el.innerHTML; }
        }(document.getElementById('msgs'));

        var ws       = new WebSocket('ws://' + window.location.host + window.location.pathname);
        console.log(ws);
        ws.onopen    = function()  { show('websocket opened'); };
        ws.onclose   = function()  { show('websocket closed'); }
        ws.onmessage = function(m) { show('websocket message: ' +  m.data); };

        var sender = function(f){
          var input     = document.getElementById('input');
          input.onclick = function(){ input.value = "" };
          f.onsubmit    = function(){
            ws.send(input.value);
            input.value = "send a message";
            return false;
          }
        }(document.getElementById('form'));
      })();
    }
  </script>
    </body>
</html>

是否有任何其他工业方法可以以这种方式进行可靠的数据传输?

我发现这很难连接。相反,划分方法可以完成这项工作。

分别独立地启动一个 websocket 服务器,Sinatra 服务器作为客户端进行通信,一个短暂的客户端,在每个传入的 TCP 请求上。

websocket 服务器将此传入消息广播到所有其他连接的客户端。