mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Format websocket doc
This commit is contained in:
		
							parent
							
								
									f258f28689
								
							
						
					
					
						commit
						40b8411f25
					
				
					 1 changed files with 17 additions and 16 deletions
				
			
		|  | @ -6,30 +6,31 @@ You can easily create a websocket handler which matches the route of `ws://host: | |||
| with different routes. | ||||
| 
 | ||||
| ```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. | ||||
| 
 | ||||
| ```ruby | ||||
|   ws "/" do |socket| | ||||
|     # Send welcome message to the client | ||||
|     socket.send "Hello from Kemal!" | ||||
| # Matches "/" | ||||
| ws "/" do |socket| | ||||
|   # Send welcome message to the client | ||||
|   socket.send "Hello from Kemal!" | ||||
| 
 | ||||
|     # Handle incoming message and echo back to the client | ||||
|     socket.on_message do |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 | ||||
|   # Handle incoming message and echo back to the client | ||||
|   socket.on_message do |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 | ||||
| ``` | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue