I'm trying to connect to remote websocket using Celluloid and Websocket client based on celluloid (gem 'celluloid-websocket-client'). The main advantage of this client for me is that I can use callbacks in the form of 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
The expected output is
"Websocket connection opened"
However, I don't get any output at all. What could be the problem?
I am using
gem 'celluloid-websocket-client', '0.0.2'
rails 4.2.1
ruby 2.1.3
As you noticed in the comments, the gem had no SSL
support. That is the trouble. To expound on the answer, here is a resolution, and also some next steps of what to expect for the future:
Celluloid::WebSocket::Client::Connection
This is an example injection to provide SSL
support to the current gem. Mine is actually highly modified, but this shows you the basic 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 through the other methods however, for example, @handler
is used to hold the calling actor, which also has the emitter methods on it. Like I said, my version is very different from the stock gem because I got fed up with it and reworked mine. But then:
Reel::IO::Client
and avoid near certain brain damage.There are exciting things going on with WebSocket
support, and a gem is coming to refactor both server and client implementations of websockets. No more monkeypatches required!
All websocket functionality is being extracted from Reel
and being combined with a websocket-driver
abstraction, as Reel::IO
... in both ::Server
and ::Client
varieties.
Interestingly, this is prompted by Rails
which is moving away from EventMachine
to Celluloid::IO
for websockets:
A prealpha is online for preview: https://github.com/celluloid/reel-io
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With