async-websocket 0.12.2 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/lib/async/websocket/adapters/rack.rb +7 -1
  3. data/lib/async/websocket/client.rb +40 -18
  4. data/lib/async/websocket/connect_request.rb +31 -17
  5. data/lib/async/websocket/connect_response.rb +2 -2
  6. data/lib/async/websocket/connection.rb +21 -2
  7. data/lib/async/websocket/request.rb +1 -1
  8. data/lib/async/websocket/response.rb +1 -1
  9. data/lib/async/websocket/server.rb +2 -0
  10. data/lib/async/websocket/upgrade_request.rb +9 -9
  11. data/lib/async/websocket/upgrade_response.rb +3 -3
  12. data/lib/async/websocket/version.rb +1 -1
  13. metadata +35 -102
  14. data/.editorconfig +0 -6
  15. data/.gitignore +0 -13
  16. data/.rspec +0 -3
  17. data/.travis.yml +0 -18
  18. data/Gemfile +0 -12
  19. data/README.md +0 -129
  20. data/Rakefile +0 -6
  21. data/async-websocket.gemspec +0 -29
  22. data/examples/chat/client.rb +0 -32
  23. data/examples/chat/config.ru +0 -113
  24. data/examples/chat/multi-client.rb +0 -81
  25. data/examples/mud/client.rb +0 -34
  26. data/examples/mud/config.ru +0 -142
  27. data/examples/rack/client.rb +0 -20
  28. data/examples/rack/config.ru +0 -14
  29. data/examples/utopia/.bowerrc +0 -4
  30. data/examples/utopia/.gitignore +0 -9
  31. data/examples/utopia/.rspec +0 -4
  32. data/examples/utopia/Gemfile +0 -33
  33. data/examples/utopia/Guardfile +0 -29
  34. data/examples/utopia/README.md +0 -16
  35. data/examples/utopia/Rakefile +0 -8
  36. data/examples/utopia/config.ru +0 -47
  37. data/examples/utopia/config/README.md +0 -7
  38. data/examples/utopia/config/environment.rb +0 -8
  39. data/examples/utopia/lib/readme.txt +0 -1
  40. data/examples/utopia/pages/_heading.xnode +0 -2
  41. data/examples/utopia/pages/_page.xnode +0 -30
  42. data/examples/utopia/pages/client/client.js +0 -28
  43. data/examples/utopia/pages/client/index.xnode +0 -8
  44. data/examples/utopia/pages/errors/exception.xnode +0 -5
  45. data/examples/utopia/pages/errors/file-not-found.xnode +0 -5
  46. data/examples/utopia/pages/links.yaml +0 -2
  47. data/examples/utopia/pages/server/controller.rb +0 -26
  48. data/examples/utopia/public/_static/icon.png +0 -0
  49. data/examples/utopia/public/_static/site.css +0 -205
  50. data/examples/utopia/public/_static/utopia-background.svg +0 -1
  51. data/examples/utopia/public/_static/utopia.svg +0 -1
  52. data/examples/utopia/public/readme.txt +0 -1
  53. data/examples/utopia/spec/spec_helper.rb +0 -31
  54. data/examples/utopia/spec/website_context.rb +0 -11
  55. data/examples/utopia/spec/website_spec.rb +0 -56
  56. data/examples/utopia/tasks/bower.rake +0 -45
  57. data/examples/utopia/tasks/deploy.rake +0 -13
  58. data/examples/utopia/tasks/development.rake +0 -34
  59. data/examples/utopia/tasks/environment.rake +0 -17
  60. data/examples/utopia/tasks/log.rake +0 -17
  61. data/examples/utopia/tasks/static.rake +0 -43
  62. data/spec/async/websocket/adapters/rack/client.rb +0 -38
  63. data/spec/async/websocket/adapters/rack/config.ru +0 -23
  64. data/spec/async/websocket/adapters/rack_spec.rb +0 -84
  65. data/spec/async/websocket/client_spec.rb +0 -48
  66. data/spec/async/websocket/connection_spec.rb +0 -34
  67. data/spec/async/websocket/server_examples.rb +0 -76
  68. data/spec/async/websocket/server_spec.rb +0 -31
  69. data/spec/async/websocket/upgrade.rb +0 -43
  70. data/spec/spec_helper.rb +0 -16
@@ -1,34 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'async'
4
- require 'async/io/stream'
5
- require 'async/http/endpoint'
6
- require 'async/websocket/client'
7
-
8
- USER = ARGV.pop || "anonymous"
9
- URL = ARGV.pop || "http://127.0.0.1:7070"
10
-
11
- Async do |task|
12
- stdin = Async::IO::Stream.new(
13
- Async::IO::Generic.new($stdin)
14
- )
15
-
16
- endpoint = Async::HTTP::Endpoint.parse(URL)
17
-
18
- Async::WebSocket::Client.open(endpoint) do |connection|
19
- task.async do
20
- $stdout.write "> "
21
-
22
- while line = stdin.read_until("\n")
23
- connection.write({input: line})
24
- connection.flush
25
-
26
- $stdout.write "> "
27
- end
28
- end
29
-
30
- while message = connection.read
31
- $stdout.puts message
32
- end
33
- end
34
- end
@@ -1,142 +0,0 @@
1
- #!/usr/bin/env falcon serve --count 1 --bind http://127.0.0.1:7070 -c
2
-
3
- require 'async/websocket/adapters/rack'
4
-
5
- class Room
6
- def initialize(name, description = nil)
7
- @name = name
8
- @description = description
9
-
10
- @actions = {}
11
- @users = []
12
- end
13
-
14
- attr :name
15
- attr :description
16
-
17
- attr :actions
18
-
19
- def connect(key, room)
20
- @actions[key] = lambda do |user|
21
- self.exit(user)
22
- room.enter(user)
23
- end
24
-
25
- return room
26
- end
27
-
28
- def broadcast(message)
29
- @users.each do |user|
30
- user.write(message)
31
- user.flush
32
- end
33
- end
34
-
35
- def enter(user)
36
- user.notify("You have entered the #{@name}.")
37
- user.room = self
38
-
39
- @users << user
40
-
41
- @users.each do |user|
42
- user.notify("#{user.name} entered the room.")
43
- end
44
- end
45
-
46
- def exit(user)
47
- if @users.delete(user)
48
- @users.each do |user|
49
- user.notify("#{user.name} left the room.")
50
- end
51
- end
52
- end
53
-
54
- def as_json
55
- {
56
- name: @name,
57
- description: @description,
58
- actions: @actions.keys,
59
- }
60
- end
61
- end
62
-
63
- module Command
64
- def self.split(line)
65
- line.scan(/(?:"")|(?:"(.*[^\\])")|(\w+)/).flatten.compact
66
- end
67
- end
68
-
69
- class User < Async::WebSocket::Connection
70
- def initialize(*)
71
- super
72
-
73
- @name = name
74
- @room = nil
75
- @inventory = []
76
- end
77
-
78
- attr_accessor :room
79
-
80
- ANONYMOUS = "Anonymous"
81
-
82
- def name
83
- @name || ANONYMOUS
84
- end
85
-
86
- def handle(message)
87
- key, *arguments = Command.split(message[:input])
88
- case key
89
- when "name"
90
- @name = arguments.first
91
- when "look"
92
- self.write({room: @room.as_json})
93
- else
94
- if action = @room.actions[key]
95
- action.call(self, *arguments)
96
- else
97
- message[:user]
98
- @room.broadcast(message)
99
- end
100
- end
101
- end
102
-
103
- def notify(text)
104
- self.write({notify: text})
105
- self.flush
106
- end
107
-
108
- def close
109
- if @room
110
- @room.exit(self)
111
- end
112
-
113
- super
114
- end
115
- end
116
-
117
- class Server
118
- def initialize(app)
119
- @app = app
120
-
121
- @entrance = Room.new("Portal", "A vast entrance foyer with a flowing portal.")
122
- @entrance.connect("forward", Room.new("Training Room"))
123
- @entrance.connect("corridor", Room.new("Shop"))
124
- end
125
-
126
- def call(env)
127
- Async::WebSocket::Adapters::Rack.open(env, connect: User) do |user|
128
- @entrance.enter(user)
129
-
130
- while message = user.read
131
- user.handle(message)
132
- end
133
- ensure
134
- # Async.logger.error(self, $!) if $!
135
- user.close
136
- end or @app.call(env)
137
- end
138
- end
139
-
140
- use Server
141
-
142
- run lambda {|env| [200, {}, []]}
@@ -1,20 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'async'
4
- require 'async/io/stream'
5
- require 'async/http/endpoint'
6
- require 'async/websocket/client'
7
-
8
- URL = ARGV.pop || "http://127.0.0.1:7070"
9
-
10
- Async do |task|
11
- endpoint = Async::HTTP::Endpoint.parse(URL)
12
-
13
- Async::WebSocket::Client.connect(endpoint) do |connection|
14
- connection.write ["Hello World"]
15
-
16
- while message = connection.read
17
- p message
18
- end
19
- end
20
- end
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env -S falcon serve --bind http://127.0.0.1:7070 --count 1 -c
2
-
3
- require 'async/websocket/adapters/rack'
4
-
5
- app = lambda do |env|
6
- Async::WebSocket::Adapters::Rack.open(env, protocols: ['ws']) do |connection|
7
- p [env["REMOTE_ADDR"], "connected", env["VERSION"]]
8
- message = connection.read
9
- p message
10
- connection.write message
11
- end
12
- end
13
-
14
- run app
@@ -1,4 +0,0 @@
1
- {
2
- "directory": "lib/components",
3
- "public": "public/_components"
4
- }
@@ -1,9 +0,0 @@
1
- # Development specific:
2
- .rspec_status
3
- .tags*
4
-
5
- # Temporary data should not be added to the repository:
6
- tmp/
7
-
8
- # This file should only ever exist on production, and may contain sensitive information:
9
- config/environment.yaml
@@ -1,4 +0,0 @@
1
- --format documentation
2
- --backtrace
3
- --warnings
4
- --require spec_helper
@@ -1,33 +0,0 @@
1
-
2
- source "https://rubygems.org"
3
-
4
- gem "utopia", "~> 2.9.0"
5
-
6
- gem "rake"
7
- gem "bundler"
8
-
9
- gem 'async-websocket'
10
-
11
- gem "rack-freeze", "~> 1.2"
12
-
13
- group :development do
14
- # For `rake server`:
15
- gem "guard-falcon", require: false
16
- gem 'guard-rspec', require: false
17
-
18
- # For `rake console`:
19
- gem "pry"
20
- gem "rack-test"
21
-
22
- # For `rspec` testing:
23
- gem "rspec"
24
- gem "simplecov"
25
-
26
- # For testing:
27
- gem 'async-rspec'
28
- end
29
-
30
- group :production do
31
- # Used for passenger-config to restart server after deployment:
32
- gem "passenger"
33
- end
@@ -1,29 +0,0 @@
1
-
2
- group :development do
3
- guard :falcon, port: 9292 do
4
- watch('Gemfile.lock')
5
- watch('config.ru')
6
- watch(%r{^config|lib|pages/.*})
7
-
8
- notification :off
9
- end
10
- end
11
-
12
- group :test do
13
- guard :rspec, cmd: 'rspec' do
14
- # Notifications can get a bit tedious:
15
- # notification :off
16
-
17
- # Re-run specs if they are changed:
18
- watch(%r{^spec/.+_spec\.rb$})
19
- watch('spec/spec_helper.rb') {'spec'}
20
-
21
- # Run relevent specs if files in `lib/` or `pages/` are changed:
22
- watch(%r{^lib/(.+)\.rb$}) {|match| "spec/lib/#{match[1]}_spec.rb" }
23
- watch(%r{^pages/(.+)\.(rb|xnode)$}) {|match| "spec/pages/#{match[1]}_spec.rb"}
24
- watch(%r{^pages/(.+)controller\.rb$}) {|match| Dir.glob("spec/pages/#{match[1]}*_spec.rb")}
25
-
26
- # If any files in pages changes, ensure the website still works:
27
- watch(%r{^pages/.*}) {'spec/website_spec.rb'}
28
- end
29
- end
@@ -1,16 +0,0 @@
1
- # Example WebSocket Chat Server
2
-
3
- This is a simple chat client/server implementation with specs.
4
-
5
- ## Starting Development Server
6
-
7
- To start the development server, simply execute
8
-
9
- > rake
10
- Generating transient session key for development...
11
- 20:57:36 - INFO - Starting Falcon HTTP server on localhost:9292
12
- 20:57:36 - INFO - Guard::RSpec is running
13
- 20:57:36 - INFO - Guard is now watching at '...'
14
- [1] guard(main)>
15
-
16
- Then browse http://localhost:9292 (or as specified) to see your new site.
@@ -1,8 +0,0 @@
1
-
2
- require 'pathname'
3
- SITE_ROOT = Pathname.new(__dir__).realpath
4
-
5
- # Load all rake tasks:
6
- import(*Dir.glob('tasks/**/*.rake'))
7
-
8
- task :default => :development
@@ -1,47 +0,0 @@
1
- #!/usr/bin/env rackup
2
-
3
- require_relative 'config/environment'
4
-
5
- require 'rack/freeze'
6
-
7
- if RACK_ENV == :production
8
- # Handle exceptions in production with a error page and send an email notification:
9
- use Utopia::Exceptions::Handler
10
- use Utopia::Exceptions::Mailer
11
- else
12
- # We want to propate exceptions up when running tests:
13
- use Rack::ShowExceptions unless RACK_ENV == :test
14
-
15
- # Serve the public directory in a similar way to the web server:
16
- use Utopia::Static, root: 'public'
17
- end
18
-
19
- use Rack::Sendfile
20
-
21
- use Utopia::ContentLength
22
-
23
- use Utopia::Redirection::Rewrite,
24
- '/' => '/client/index'
25
-
26
- use Utopia::Redirection::DirectoryIndex
27
-
28
- use Utopia::Redirection::Errors,
29
- 404 => '/errors/file-not-found'
30
-
31
- use Utopia::Localization,
32
- :default_locale => 'en',
33
- :locales => ['en', 'de', 'ja', 'zh']
34
-
35
- require 'utopia/session'
36
- use Utopia::Session,
37
- :expires_after => 3600 * 24,
38
- :secret => ENV['UTOPIA_SESSION_SECRET']
39
-
40
- use Utopia::Controller
41
-
42
- use Utopia::Static
43
-
44
- # Serve dynamic content
45
- use Utopia::Content
46
-
47
- run lambda { |env| [404, {}, []] }
@@ -1,7 +0,0 @@
1
- # Utopia Config
2
-
3
- This directory contains `environment.rb` which is used to initialize the running environment for tasks and servers.
4
-
5
- ## Setting Environment Variables
6
-
7
- If you wish to set environment variables on a per-deployment basis, you can do so by creating an `config/environment.yaml` and populating it with key-value pairs.
@@ -1,8 +0,0 @@
1
-
2
- require 'bundler/setup'
3
- Bundler.setup
4
-
5
- require 'utopia/setup'
6
- Utopia.setup
7
-
8
- RACK_ENV = ENV.fetch('RACK_ENV', :development).to_sym unless defined? RACK_ENV
@@ -1 +0,0 @@
1
- You can add additional code for your application in this directory, and require it directly from the config.ru.
@@ -1,2 +0,0 @@
1
- <?r document.attributes[:title] ||= content ?>
2
- <h1><utopia:content/></h1>
@@ -1,30 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <?r response.cache! ?>
5
-
6
- <?r if title = self[:title] ?>
7
- <title>#{title.gsub(/<.*?>/, "")} - Utopia</title>
8
- <?r else ?>
9
- <title>Utopia</title>
10
- <?r end ?>
11
-
12
- <base href="#{first.node.uri_path}"/>
13
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
14
-
15
- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous" />
16
-
17
- <link rel="icon" type="image/png" href="/_static/icon.png" />
18
- <link rel="stylesheet" href="/_static/site.css" type="text/css" media="screen" />
19
- </head>
20
-
21
- <body class="#{attributes[:class]}">
22
- <header>
23
- <img src="/_static/utopia.svg" />
24
- </header>
25
-
26
- <div id="page">
27
- <utopia:content/>
28
- </div>
29
- </body>
30
- </html>
@@ -1,28 +0,0 @@
1
-
2
- var url = new URL('/server/connect', window.location.href);
3
- url.protocol = url.protocol.replace('http', 'ws');
4
-
5
- console.log("Connecting to server", url);
6
- var server = new WebSocket(url.href);
7
- console.log("Connected to", server);
8
-
9
- server.onopen = function(event) {
10
- chat.onkeypress = function(event) {
11
- if (event.keyCode == 13) {
12
- server.send(JSON.stringify({text: chat.value}));
13
-
14
- chat.value = "";
15
- }
16
- }
17
- };
18
-
19
- server.onmessage = function(event) {
20
- console.log("Got message", event);
21
-
22
- var message = JSON.parse(event.data);
23
-
24
- var pre = document.createElement('pre');
25
- pre.innerText = message.text;
26
-
27
- response.appendChild(pre);
28
- };