0

I have a Websocket server (using Rubame), and it handles server connections in block form:

server.run do |client|
    # ...
end

The client sends a message (i.e. ping;end) to the server, then the server sends a message back (i.e. pong;end).

Instead of having my react-to-message code inside the block, creating a huge, ugly mess, I want to have it in a function:

def react(msg)
    # ...
end
server.run do |client|
    client.onmessage do |mess|
        react(mess)
    end
end

To send a message back to the client, I need to access the client variable passed to the server.run block, and it appears to not be accessible from within the method.

Is there a way to access the variables of a block from within a method which was called inside the block?

1 Answer 1

4

Just pass client along to your method:

def react(client, msg)
  # ...
end

server.run do |client|
  client.onmessage do |mess|
    react(client, mess)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.