Format websocket doc

This commit is contained in:
Sdogruyol 2015-12-16 20:21:17 +02:00
parent f258f28689
commit 40b8411f25

View file

@ -6,30 +6,31 @@ You can easily create a websocket handler which matches the route of `ws://host:
with different routes. with different routes.
```ruby ```ruby
ws "/" do |socket| ws "/" do |socket|
end end
ws "/route2" do |socket| ws "/route2" do |socket|
end end
``` ```
Let's access the socket and create a simple echo server. Let's access the socket and create a simple echo server.
```ruby ```ruby
ws "/" do |socket| # Matches "/"
# Send welcome message to the client ws "/" do |socket|
socket.send "Hello from Kemal!" # Send welcome message to the client
socket.send "Hello from Kemal!"
# Handle incoming message and echo back to the client # Handle incoming message and echo back to the client
socket.on_message do |message| socket.on_message do |message|
socket.send "Echo back from server #{message}" socket.send "Echo back from server #{message}"
end
# Executes when the client is disconnected. You can do the cleaning up here.
socket.on_close do
puts "Closing socket"
end
end end
# Executes when the client is disconnected. You can do the cleaning up here.
socket.on_close do
puts "Closing socket"
end
end
``` ```