async-websocket 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/async/websocket/adapters/rack.rb +1 -1
- data/lib/async/websocket/client.rb +7 -1
- data/lib/async/websocket/version.rb +1 -1
- metadata +60 -84
- data/.editorconfig +0 -6
- data/.gitignore +0 -13
- data/.rspec +0 -3
- data/.travis.yml +0 -21
- data/Gemfile +0 -7
- data/README.md +0 -143
- data/async-websocket.gemspec +0 -29
- data/examples/chat/.env +0 -3
- data/examples/chat/README.md +0 -147
- data/examples/chat/client.rb +0 -32
- data/examples/chat/config.ru +0 -150
- data/examples/chat/multi-client.rb +0 -79
- data/examples/mud/client.rb +0 -34
- data/examples/mud/config.ru +0 -142
- data/examples/rack/client.rb +0 -19
- data/examples/rack/config.ru +0 -12
- data/examples/utopia/.bowerrc +0 -4
- data/examples/utopia/.gitignore +0 -9
- data/examples/utopia/.rspec +0 -4
- data/examples/utopia/Gemfile +0 -33
- data/examples/utopia/Guardfile +0 -29
- data/examples/utopia/README.md +0 -16
- data/examples/utopia/Rakefile +0 -8
- data/examples/utopia/config.ru +0 -47
- data/examples/utopia/config/README.md +0 -7
- data/examples/utopia/config/environment.rb +0 -8
- data/examples/utopia/lib/readme.txt +0 -1
- data/examples/utopia/pages/_heading.xnode +0 -2
- data/examples/utopia/pages/_page.xnode +0 -30
- data/examples/utopia/pages/client/client.js +0 -28
- data/examples/utopia/pages/client/index.xnode +0 -8
- data/examples/utopia/pages/errors/exception.xnode +0 -5
- data/examples/utopia/pages/errors/file-not-found.xnode +0 -5
- data/examples/utopia/pages/links.yaml +0 -2
- data/examples/utopia/pages/server/controller.rb +0 -26
- data/examples/utopia/public/_static/icon.png +0 -0
- data/examples/utopia/public/_static/site.css +0 -205
- data/examples/utopia/public/_static/utopia-background.svg +0 -1
- data/examples/utopia/public/_static/utopia.svg +0 -1
- data/examples/utopia/public/readme.txt +0 -1
- data/examples/utopia/spec/spec_helper.rb +0 -31
- data/examples/utopia/spec/website_context.rb +0 -11
- data/examples/utopia/spec/website_spec.rb +0 -56
- data/examples/utopia/tasks/bower.rake +0 -45
- data/examples/utopia/tasks/deploy.rake +0 -13
- data/examples/utopia/tasks/development.rake +0 -34
- data/examples/utopia/tasks/environment.rake +0 -17
- data/examples/utopia/tasks/log.rake +0 -17
- data/examples/utopia/tasks/static.rake +0 -43
- data/spec/async/websocket/adapters/rack/client.rb +0 -38
- data/spec/async/websocket/adapters/rack/config.ru +0 -23
- data/spec/async/websocket/adapters/rack_spec.rb +0 -84
- data/spec/async/websocket/connection_spec.rb +0 -34
- data/spec/async/websocket/server_examples.rb +0 -117
- data/spec/async/websocket/server_spec.rb +0 -31
- data/spec/async/websocket/upgrade.rb +0 -43
- data/spec/spec_helper.rb +0 -16
data/examples/mud/config.ru
DELETED
@@ -1,142 +0,0 @@
|
|
1
|
-
#!/usr/bin/env -S 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, handler: 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, {}, []]}
|
data/examples/rack/client.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'async'
|
4
|
-
require 'async/http/endpoint'
|
5
|
-
require 'async/websocket/client'
|
6
|
-
|
7
|
-
URL = ARGV.pop || "http://127.0.0.1:7070"
|
8
|
-
|
9
|
-
Async do |task|
|
10
|
-
endpoint = Async::HTTP::Endpoint.parse(URL)
|
11
|
-
|
12
|
-
Async::WebSocket::Client.connect(endpoint) do |connection|
|
13
|
-
connection.write ["Hello", "World"]
|
14
|
-
|
15
|
-
while message = connection.read
|
16
|
-
p message
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/examples/rack/config.ru
DELETED
@@ -1,12 +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) do |connection|
|
7
|
-
message = connection.read
|
8
|
-
connection.write message.reverse
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
run app
|
data/examples/utopia/.bowerrc
DELETED
data/examples/utopia/.gitignore
DELETED
data/examples/utopia/.rspec
DELETED
data/examples/utopia/Gemfile
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
|
2
|
-
source "https://rubygems.org"
|
3
|
-
|
4
|
-
gem "utopia", "~> 2.9.3"
|
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
|
data/examples/utopia/Guardfile
DELETED
@@ -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
|
data/examples/utopia/README.md
DELETED
@@ -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.
|
data/examples/utopia/Rakefile
DELETED
data/examples/utopia/config.ru
DELETED
@@ -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 +0,0 @@
|
|
1
|
-
You can add additional code for your application in this directory, and require it directly from the config.ru.
|
@@ -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
|
-
};
|