Ruby: connecting to a remote WebSocket

I am trying to connect to a remote websocket using Celluloid and a celluloid -based Websocket client ( gem 'celluloid-websocket-client' ). The main advantage of this client for me is that I can use callbacks as class methods instead of blocks.

require 'celluloid/websocket/client' class WSConnection include Celluloid def initialize(url) @ws_client = Celluloid::WebSocket::Client.new url, Celluloid::Actor.current end # When WebSocket is opened, register callbacks def on_open puts "Websocket connection opened" end # When raw WebSocket message is received def on_message(msg) puts "Received message: #{msg}" end # When WebSocket is closed def on_close(code, reason) puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}" end end m = WSConnection.new('wss://foo.bar') while true; sleep; end 

Expected Result:

 "Websocket connection opened" 

However, I am not getting any output at all. What could be the problem?

I use

 gem 'celluloid-websocket-client', '0.0.2' rails 4.2.1 ruby 2.1.3 
+5
source share
1 answer

As you noticed in the comments, the gem did not have SSL support. That is the problem. To state the answer, here is a resolution, as well as some next steps that can be expected in the future:


[now] Override methods in Celluloid::WebSocket::Client::Connection

This is an injection example to provide SSL support for the current gem. In fact, the mine is heavily modified, but this shows you the main solution:

 def initialize(url, handler=nil) @url = url @handler = handler || Celluloid::Actor.current #de If you want an auto-start: start end def start uri = URI.parse(@url) port = uri.port || (uri.scheme == "ws" ? 80 : 443) @socket.close rescue nil @socket = Celluloid::IO::TCPSocket.new(uri.host, port) @socket = Celluloid::IO::SSLSocket.new(@socket) if port == 443 @socket.connect @client = ::WebSocket::Driver.client(self) async.run end 

The above sends ripple effects in other ways, for example, @handler used to hold the caller who also has emitter methods on it. As I said, my version is very different from the stock, because I was tired of it and reworked mine. But then:


[soon] Use Reel::IO::Client and avoid some brain damage.

Interesting things are happening with WebSocket support, and gem is currently being used to reorganize server and client implementations of Web sites. No more monkeypatches required!

All websocket functionality is retrieved from Reel and combined with the websocket-driver abstraction, like Reel::IO ... in the options ::Server and ::Client .

Interestingly, this is caused by Rails , which departs from EventMachine to Celluloid::IO for websockets:

Prealpha is online to preview: https://github.com/celluloid/reel-io

+4
source

All Articles