Hello,
This is kinda two questions in one.
First of all, how can you detach a WebSocket connection when using Webmachine? There are no examples of this and I suspect it isn't possible because the websocket_handler callback only passes the WebSocket. There is no way to get the associated connection from it. I could file a pull request to add it.
Second, what's the best way to continually read from the WebSocket? The roundtrip.rb example appears to poll the socket every second, which isn't great. The read call blocks so it's not all that bad but still a strange way to do it. I tried this.
Webmachine::Application.new do |app|
app.configure do |config|
config.port = 8080
config.adapter = :Reel
config.adapter_options[:websocket_handler] = proc do |websocket|
SocketHandler.new(websocket)
end
end
end
class SocketHandler
include Celluloid
def initialize(websocket)
@socket = websocket
async.run
end
def run
loop do
data = @socket.read
# Do stuff.
end
rescue IOError
end
end
It does work but it still doesn't feel right, especially after I saw the on_message method. I then tried this but it never fires. What am I doing wrong?
class SocketHandler
include Celluloid
def initialize(websocket)
@socket = websocket
@socket.on_message { |data| receive data }
end
def receive(data)
# Do stuff.
end
end