8000 Add some more examples in README. · destructobeam/async-websocket@fb61f54 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb61f54

Browse files
committed
Add some more examples in README.
1 parent a5b622d commit fb61f54

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,44 @@ Or install it yourself as:
2222

2323
## Usage
2424

25-
Here is how to use within `Rack`:
25+
There are [examples](examples/) which include:
26+
27+
- [A command line chat client and application server](examples/chat/client.rb) which can read input from `stdin` and send messages to the server.
28+
- [A utopia-based web application](examples/utopia) which uses a JavaScript client to connect to a web application server.
29+
30+
### Client Side with Async
31+
32+
```ruby
33+
#!/usr/bin/env ruby
34+
35+
require 'async'
36+
require 'async/io/stream'
37+
require 'async/http/url_endpoint'
38+
require 'async/websocket/client'
39+
40+
USER = ARGV.pop || "anonymous"
41+
URL = ARGV.pop || "ws://localhost:9292"
42+
43+
Async do |task|
44+
endpoint = Async::HTTP::URLEndpoint.parse(URL)
45+
headers = {'token' => 'wubalubadubdub'}
46+
47+
endpoint.connect do |socket|
48+
connection = Async::WebSocket::Client.new(socket, URL, headers)
49+
50+
connection.send_message({
51+
user: USER,
52+
status: "connected",
53+
})
54+
55+
while message = connection.next_message
56+
puts message.inspect
57+
end
58+
end
59+
end
60+
```
61+
62+
### Server Side with Rack & Falcon
2663

2764
```ruby
2865
#!/usr/bin/env falcon serve --concurrency 1 -c
@@ -46,8 +83,6 @@ run lambda {|env|
4683
}
4784
```
4885

49-
And [here is a client program](examples/chat/client.rb) which can read input from `stdin` and send messages to the server.
50-
5186
## Contributing
5287

5388
1. Fork it

0 commit comments

Comments
 (0)
0