Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit 19e0861

Browse files
committed
Add udp example.
1 parent 4c5e349 commit 19e0861

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

examples/udp.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'async'
4+
require_relative '../lib/async/io'
5+
6+
endpoint = Async::IO::Endpoint.udp("localhost", 5300)
7+
8+
Async do |task|
9+
endpoint.bind do |socket|
10+
# This block executes for both IPv4 and IPv6 UDP sockets:
11+
loop do
12+
data, address = socket.recvfrom(1024)
13+
pp data
14+
pp address
15+
end
16+
end
17+
18+
# This will try connecting to all addresses and yield for the first one that successfully connects:
19+
endpoint.connect do |socket|
20+
loop do
21+
task.sleep rand(1..10)
22+
socket.send "Hello World!", 0
23+
end
24+
end
25+
end
26+

0 commit comments

Comments
 (0)