Monday, 15 March 2010

Establish a WebSocket connection in HTTP server (Sinatra Stream) in Ruby -


I write an HTTP server (A) using Sinatra :: Streaming (I use Ruby). It needs to read data from WebSketake Server (B) and forward it to client (C). Data flow is "B -> A -> C" Read and write operations should be in real time.

As a possible solution, I tried to make a WebSocket connection in Sinatra. The code is as follows:

  Deploy class   Sinatra :: Base Helpers Sinatra :: Streaming Set: Sessions, True Set: Get the Server, 'Thin', '/' Stream: (keep_open). Outside | # Creating a WebSocket Connection EM.run {ws = Faye :: WebSocket :: Client.new ('ws: //0.0.0.0: 8080 /') ws.on: Message | Event | Outside & lt; & Lt; Event.data out.flush end ws.on: Stop | Event | Ws = nil end} end end  

I have tried several WebSocket client labs such as the FE-websocket program ~ / .rvm / gems / ruby-2.0.0- P598 / gemstone / sinatra-contrib-1.4.2 / lib / sinatra / streaming.rb: 100: in '& lt; & Lt; & gt; Has failed as: not opened for writing (IOError) while executing outside . WebSocket connection was locked at the same time.

It seems that the IOS of WebSocket shutters / closes the I / S stream of the stream.

I currently allow a sub-project to set up a WebSocket connection, and the subprakash to interact with Sinatra (A). But if many system resources can be consumed, if tens of people reach A What's more, the Sinatra stream server is often closed for write (IOError) when there are too many clients or too long wait times.

So my question is

  1. How to implement a websacet client in an HTTP stream server? (It is okay to use other Libra instead of Sinatra)
  2. If a WebSocket client can be implemented in the Sinatra stream, how to improve the stability of the Sinatra stream? (Avoid IOError)

Sorry for my bad english Thank you very much!

The problem occurred a while ago, and I forgot the details of the solution. But according to my weak memory, you can update the Ruby version (> = Ruby-2) to avoid this error. I added several "sleeping" microsounds between each two "written".

I believe that Ruby's version is the main reason. Sorry I can not test it now


No comments:

Post a Comment