Reputation: 366
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?
Upvotes: 0
Views: 51
Reputation: 17323
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
Upvotes: 4