polyphony 0.19 → 0.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.rubocop.yml +87 -1
- data/CHANGELOG.md +35 -0
- data/Gemfile.lock +17 -6
- data/README.md +200 -139
- data/Rakefile +4 -4
- data/TODO.md +35 -7
- data/bin/poly +11 -0
- data/docs/getting-started/getting-started.md +1 -1
- data/docs/summary.md +3 -0
- data/docs/technical-overview/exception-handling.md +94 -0
- data/docs/technical-overview/fiber-scheduling.md +99 -0
- data/examples/core/cancel.rb +8 -4
- data/examples/core/channel_echo.rb +18 -17
- data/examples/core/defer.rb +12 -0
- data/examples/core/enumerator.rb +4 -4
- data/examples/core/fiber_error.rb +9 -0
- data/examples/core/fiber_error_with_backtrace.rb +73 -0
- data/examples/core/fork.rb +6 -6
- data/examples/core/genserver.rb +16 -8
- data/examples/core/lock.rb +3 -3
- data/examples/core/move_on.rb +4 -3
- data/examples/core/move_on_twice.rb +5 -5
- data/examples/core/move_on_with_ensure.rb +8 -11
- data/examples/core/move_on_with_value.rb +14 -0
- data/examples/core/{multiple_spawn.rb → multiple_spin.rb} +5 -5
- data/examples/core/nested_cancel.rb +5 -5
- data/examples/core/{nested_multiple_spawn.rb → nested_multiple_spin.rb} +6 -6
- data/examples/core/nested_spin.rb +17 -0
- data/examples/core/pingpong.rb +21 -0
- data/examples/core/pulse.rb +4 -5
- data/examples/core/resource.rb +6 -4
- data/examples/core/resource_cancel.rb +6 -9
- data/examples/core/resource_delegate.rb +3 -3
- data/examples/core/sleep.rb +3 -3
- data/examples/core/sleep_spin.rb +19 -0
- data/examples/core/snooze.rb +32 -0
- data/examples/core/spin.rb +14 -0
- data/examples/core/{spawn_cancel.rb → spin_cancel.rb} +6 -7
- data/examples/core/spin_error.rb +17 -0
- data/examples/core/spin_error_backtrace.rb +30 -0
- data/examples/core/spin_uncaught_error.rb +15 -0
- data/examples/core/supervisor.rb +8 -8
- data/examples/core/supervisor_with_cancel_scope.rb +7 -7
- data/examples/core/supervisor_with_error.rb +8 -8
- data/examples/core/supervisor_with_manual_move_on.rb +6 -7
- data/examples/core/suspend.rb +13 -0
- data/examples/core/thread.rb +1 -1
- data/examples/core/thread_cancel.rb +9 -11
- data/examples/core/thread_pool.rb +18 -14
- data/examples/core/throttle.rb +7 -7
- data/examples/core/timeout.rb +3 -3
- data/examples/fs/read.rb +7 -9
- data/examples/http/config.ru +7 -3
- data/examples/http/cuba.ru +22 -0
- data/examples/http/happy_eyeballs.rb +6 -4
- data/examples/http/http_client.rb +1 -1
- data/examples/http/http_get.rb +1 -1
- data/examples/http/http_parse_experiment.rb +21 -16
- data/examples/http/http_proxy.rb +28 -26
- data/examples/http/http_server.rb +10 -10
- data/examples/http/http_server_forked.rb +6 -5
- data/examples/http/http_server_throttled.rb +3 -3
- data/examples/http/http_ws_server.rb +11 -11
- data/examples/http/https_raw_client.rb +1 -1
- data/examples/http/https_server.rb +8 -8
- data/examples/http/https_wss_server.rb +13 -11
- data/examples/http/rack_server.rb +2 -2
- data/examples/http/rack_server_https.rb +4 -4
- data/examples/http/rack_server_https_forked.rb +5 -5
- data/examples/http/websocket_secure_server.rb +6 -6
- data/examples/http/websocket_server.rb +5 -5
- data/examples/interfaces/pg_client.rb +4 -4
- data/examples/interfaces/pg_pool.rb +13 -6
- data/examples/interfaces/pg_transaction.rb +5 -4
- data/examples/interfaces/redis_channels.rb +15 -11
- data/examples/interfaces/redis_client.rb +2 -2
- data/examples/interfaces/redis_pubsub.rb +2 -1
- data/examples/interfaces/redis_pubsub_perf.rb +13 -9
- data/examples/io/backticks.rb +11 -0
- data/examples/io/cat.rb +4 -5
- data/examples/io/echo_client.rb +9 -4
- data/examples/io/echo_client_from_stdin.rb +20 -0
- data/examples/io/echo_pipe.rb +7 -8
- data/examples/io/echo_server.rb +8 -6
- data/examples/io/echo_server_with_timeout.rb +13 -10
- data/examples/io/echo_stdin.rb +3 -3
- data/examples/io/httparty.rb +2 -2
- data/examples/io/httparty_multi.rb +8 -4
- data/examples/io/httparty_threaded.rb +6 -2
- data/examples/io/io_read.rb +2 -2
- data/examples/io/irb.rb +16 -4
- data/examples/io/net-http.rb +3 -3
- data/examples/io/open.rb +17 -0
- data/examples/io/system.rb +3 -3
- data/examples/io/tcpserver.rb +15 -0
- data/examples/io/tcpsocket.rb +6 -5
- data/examples/performance/multi_snooze.rb +29 -0
- data/examples/performance/{perf_snooze.rb → snooze.rb} +7 -5
- data/examples/performance/snooze_raw.rb +39 -0
- data/ext/gyro/async.c +165 -0
- data/ext/gyro/child.c +167 -0
- data/ext/{ev → gyro}/extconf.rb +4 -3
- data/ext/gyro/gyro.c +316 -0
- data/ext/{ev/ev.h → gyro/gyro.h} +12 -7
- data/ext/gyro/gyro_ext.c +23 -0
- data/ext/{ev → gyro}/io.c +65 -57
- data/ext/{ev → gyro}/libev.h +0 -0
- data/ext/gyro/signal.c +117 -0
- data/ext/{ev → gyro}/socket.c +61 -6
- data/ext/gyro/timer.c +199 -0
- data/ext/libev/Changes +35 -0
- data/ext/libev/README +2 -1
- data/ext/libev/ev.c +213 -151
- data/ext/libev/ev.h +95 -88
- data/ext/libev/ev_epoll.c +26 -15
- data/ext/libev/ev_kqueue.c +11 -5
- data/ext/libev/ev_linuxaio.c +642 -0
- data/ext/libev/ev_poll.c +13 -8
- data/ext/libev/ev_port.c +5 -2
- data/ext/libev/ev_vars.h +14 -3
- data/ext/libev/ev_wrap.h +16 -0
- data/lib/ev_ext.bundle +0 -0
- data/lib/polyphony.rb +46 -50
- data/lib/polyphony/auto_run.rb +12 -0
- data/lib/polyphony/core/cancel_scope.rb +11 -7
- data/lib/polyphony/core/channel.rb +16 -9
- data/lib/polyphony/core/coprocess.rb +101 -51
- data/lib/polyphony/core/exceptions.rb +14 -12
- data/lib/polyphony/core/resource_pool.rb +21 -8
- data/lib/polyphony/core/supervisor.rb +10 -5
- data/lib/polyphony/core/sync.rb +7 -6
- data/lib/polyphony/core/thread.rb +4 -4
- data/lib/polyphony/core/thread_pool.rb +4 -4
- data/lib/polyphony/core/throttler.rb +6 -4
- data/lib/polyphony/extensions/core.rb +253 -0
- data/lib/polyphony/extensions/io.rb +28 -16
- data/lib/polyphony/extensions/openssl.rb +2 -1
- data/lib/polyphony/extensions/socket.rb +47 -52
- data/lib/polyphony/http.rb +4 -3
- data/lib/polyphony/http/agent.rb +68 -57
- data/lib/polyphony/http/server.rb +5 -5
- data/lib/polyphony/http/server/http1.rb +268 -0
- data/lib/polyphony/http/server/http2.rb +62 -0
- data/lib/polyphony/http/server/http2_stream.rb +104 -0
- data/lib/polyphony/http/server/rack.rb +64 -0
- data/lib/polyphony/http/server/request.rb +119 -0
- data/lib/polyphony/net.rb +26 -15
- data/lib/polyphony/postgres.rb +17 -13
- data/lib/polyphony/redis.rb +16 -15
- data/lib/polyphony/version.rb +1 -1
- data/lib/polyphony/websocket.rb +11 -4
- data/polyphony.gemspec +13 -9
- data/test/eg.rb +27 -0
- data/test/helper.rb +25 -0
- data/test/run.rb +5 -0
- data/test/test_async.rb +33 -0
- data/test/test_coprocess.rb +239 -77
- data/test/test_core.rb +95 -61
- data/test/test_gyro.rb +148 -0
- data/test/test_http_server.rb +313 -0
- data/test/test_io.rb +79 -27
- data/test/test_kernel.rb +22 -12
- data/test/test_signal.rb +36 -0
- data/test/test_timer.rb +24 -0
- metadata +89 -33
- data/examples/core/nested_async.rb +0 -17
- data/examples/core/next_tick.rb +0 -12
- data/examples/core/sleep_spawn.rb +0 -19
- data/examples/core/spawn.rb +0 -14
- data/examples/core/spawn_error.rb +0 -28
- data/examples/performance/perf_multi_snooze.rb +0 -21
- data/ext/ev/async.c +0 -168
- data/ext/ev/child.c +0 -169
- data/ext/ev/ev_ext.c +0 -23
- data/ext/ev/ev_module.c +0 -242
- data/ext/ev/signal.c +0 -119
- data/ext/ev/timer.c +0 -197
- data/lib/polyphony/core/fiber_pool.rb +0 -98
- data/lib/polyphony/extensions/kernel.rb +0 -169
- data/lib/polyphony/http/http1_adapter.rb +0 -254
- data/lib/polyphony/http/http2_adapter.rb +0 -157
- data/lib/polyphony/http/rack.rb +0 -25
- data/lib/polyphony/http/request.rb +0 -66
- data/test/test_ev.rb +0 -110
data/examples/http/config.ru
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuba'
|
4
|
+
require 'cuba/safe'
|
5
|
+
|
6
|
+
Cuba.use Rack::Session::Cookie, secret: '__a_very_long_string__'
|
7
|
+
|
8
|
+
Cuba.plugin Cuba::Safe
|
9
|
+
|
10
|
+
Cuba.define do
|
11
|
+
on get do
|
12
|
+
on 'hello' do
|
13
|
+
res.write 'Hello world!'
|
14
|
+
end
|
15
|
+
|
16
|
+
on root do
|
17
|
+
res.redirect '/hello'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
run Cuba
|
@@ -6,11 +6,13 @@
|
|
6
6
|
require 'bundler/setup'
|
7
7
|
require 'polyphony/http'
|
8
8
|
|
9
|
-
|
9
|
+
def try_connect(target, supervisor)
|
10
10
|
puts "trying #{target[2]}"
|
11
11
|
socket = Polyphony::Net.tcp_connect(target[2], 80)
|
12
|
+
# connection successful
|
12
13
|
supervisor.stop!([target[2], socket])
|
13
14
|
rescue IOError, SystemCallError
|
15
|
+
# ignore error
|
14
16
|
end
|
15
17
|
|
16
18
|
def happy_eyeballs(hostname, port, max_wait_time: 0.025)
|
@@ -20,11 +22,11 @@ def happy_eyeballs(hostname, port, max_wait_time: 0.025)
|
|
20
22
|
success = supervise do |supervisor|
|
21
23
|
targets.each_with_index do |t, idx|
|
22
24
|
sleep(max_wait_time) if idx > 0
|
23
|
-
supervisor.
|
25
|
+
supervisor.spin { try_connect(t, supervisor) }
|
24
26
|
end
|
25
27
|
end
|
26
28
|
if success
|
27
|
-
puts
|
29
|
+
puts format('success: %s (%.3fs)', success[0], Time.now - t0)
|
28
30
|
else
|
29
31
|
puts "timed out (#{Time.now - t0}s)"
|
30
32
|
end
|
@@ -32,4 +34,4 @@ def happy_eyeballs(hostname, port, max_wait_time: 0.025)
|
|
32
34
|
end
|
33
35
|
|
34
36
|
# Let's try it out:
|
35
|
-
happy_eyeballs(
|
37
|
+
happy_eyeballs('debian.org', 'https')
|
data/examples/http/http_get.rb
CHANGED
@@ -17,7 +17,7 @@ class ParseLoop
|
|
17
17
|
@parser << data
|
18
18
|
snooze
|
19
19
|
end
|
20
|
-
rescue => e
|
20
|
+
rescue StandardError => e
|
21
21
|
conn.close
|
22
22
|
e
|
23
23
|
ensure
|
@@ -56,7 +56,7 @@ class ParseLoop
|
|
56
56
|
|
57
57
|
def consume_request
|
58
58
|
return unless @message_in_train
|
59
|
-
|
59
|
+
|
60
60
|
@calling_fiber = Fiber.current
|
61
61
|
@read_body = false
|
62
62
|
@parse_fiber.safe_transfer while @message_in_train
|
@@ -74,24 +74,25 @@ end
|
|
74
74
|
def handle(parser)
|
75
75
|
headers = parser.parse_headers
|
76
76
|
return unless headers
|
77
|
+
|
77
78
|
puts "headers: #{headers.inspect}"
|
78
|
-
|
79
|
+
headers['Content-Length']
|
79
80
|
# if content_length && (content_length.to_i < 1000)
|
80
|
-
|
81
|
-
|
82
|
-
|
81
|
+
while (chunk = parser.parse_body_chunk)
|
82
|
+
puts "chunk: #{chunk.inspect}"
|
83
|
+
end
|
83
84
|
# else
|
84
85
|
# parser.consume_request
|
85
86
|
# end
|
86
|
-
puts
|
87
|
-
rescue => e
|
87
|
+
puts 'end of request'
|
88
|
+
rescue StandardError => e
|
88
89
|
puts "error: #{e.inspect}"
|
89
90
|
raise e
|
90
91
|
end
|
91
92
|
|
92
|
-
writer = spin
|
93
|
+
writer = spin do
|
93
94
|
o << "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 6\r\n\r\n"
|
94
|
-
o <<
|
95
|
+
o << 'Hello!'
|
95
96
|
|
96
97
|
o << "POST / HTTP/1.1\r\nHost: example.com\r\n\r\n"
|
97
98
|
|
@@ -99,20 +100,24 @@ writer = spin {
|
|
99
100
|
# o << ("Bye!" * 2048)
|
100
101
|
|
101
102
|
o << "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 4\r\n\r\n"
|
102
|
-
o <<
|
103
|
+
o << 'Bye!'
|
103
104
|
|
104
|
-
|
105
|
+
begin
|
106
|
+
(o << ('BLAH' * 100_000))
|
107
|
+
rescue StandardError
|
108
|
+
nil
|
109
|
+
end
|
105
110
|
o.close
|
106
|
-
|
111
|
+
end
|
107
112
|
|
108
113
|
begin
|
109
114
|
parse_loop = ParseLoop.new(i)
|
110
115
|
while parse_loop.alive?
|
111
|
-
puts
|
116
|
+
puts '*' * 40
|
112
117
|
handle(parse_loop)
|
113
118
|
end
|
114
|
-
rescue => e
|
119
|
+
rescue StandardError => e
|
115
120
|
writer.stop
|
116
121
|
puts "#{e.class}: #{e.message}"
|
117
122
|
puts e.backtrace
|
118
|
-
end
|
123
|
+
end
|
data/examples/http/http_proxy.rb
CHANGED
@@ -8,32 +8,30 @@ require 'localhost/authority'
|
|
8
8
|
|
9
9
|
BASE_URL = 'http://realiteq.net'
|
10
10
|
|
11
|
-
CACHE = {}
|
11
|
+
CACHE = {}.freeze
|
12
12
|
|
13
13
|
def proxy(uri, opts)
|
14
14
|
now = Time.now
|
15
15
|
uri = BASE_URL + uri
|
16
16
|
entry = CACHE[uri]
|
17
|
-
if entry && entry[:expires] >= now
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
response
|
27
|
-
end
|
17
|
+
return entry[:response] if entry && entry[:expires] >= now
|
18
|
+
|
19
|
+
puts "proxy => #{uri} (#{opts.inspect})"
|
20
|
+
response = Polyphony::HTTP::Agent.get(uri, opts)
|
21
|
+
# CACHE[uri] = {
|
22
|
+
# expires: now + 60,
|
23
|
+
# response: response
|
24
|
+
# }
|
25
|
+
response
|
28
26
|
end
|
29
27
|
|
30
|
-
HEADERS_BLACK_LIST = %w
|
28
|
+
HEADERS_BLACK_LIST = %w[
|
31
29
|
Transfer-Encoding Date Server Connection Content-Length Cache-Control
|
32
30
|
:method :authority :scheme :path
|
33
|
-
|
31
|
+
].freeze
|
34
32
|
|
35
33
|
def sanitize_headers(headers)
|
36
|
-
headers.reject { |k,
|
34
|
+
headers.reject { |k, _v| HEADERS_BLACK_LIST.include?(k) }
|
37
35
|
end
|
38
36
|
|
39
37
|
def sanitize_html(html)
|
@@ -42,20 +40,24 @@ end
|
|
42
40
|
|
43
41
|
# authority = Localhost::Authority.fetch
|
44
42
|
|
45
|
-
rsa_cert = OpenSSL::X509::Certificate.new(
|
46
|
-
|
43
|
+
rsa_cert = OpenSSL::X509::Certificate.new(
|
44
|
+
IO.read('../reality/aws/config/ssl/full_chain.pem')
|
45
|
+
)
|
46
|
+
rsa_pkey = OpenSSL::PKey.read(
|
47
|
+
IO.read('../reality/aws/config/ssl/private_key.pem')
|
48
|
+
)
|
47
49
|
ctx = OpenSSL::SSL::SSLContext.new
|
48
50
|
ctx.add_certificate(rsa_cert, rsa_pkey)
|
49
51
|
|
50
52
|
opts = {
|
51
|
-
reuse_addr:
|
52
|
-
dont_linger:
|
53
|
-
secure_context: ctx#authority.server_context
|
53
|
+
reuse_addr: true,
|
54
|
+
dont_linger: true,
|
55
|
+
secure_context: ctx # authority.server_context
|
54
56
|
}
|
55
57
|
|
56
|
-
spin
|
58
|
+
spin do
|
57
59
|
Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
|
58
|
-
puts "#{req.method} #{req.uri
|
60
|
+
puts "#{req.method} #{req.uri}"
|
59
61
|
puts "headers <: #{req.headers.inspect}"
|
60
62
|
# h = {
|
61
63
|
# uri: req.uri.to_s,
|
@@ -70,12 +72,12 @@ spin {
|
|
70
72
|
puts "headers >: #{response[:headers].inspect}"
|
71
73
|
# body = sanitize_html(body) if headers['Content-Type'] =~ /text\/html/
|
72
74
|
req.respond(body, headers)
|
73
|
-
rescue => e
|
74
|
-
puts
|
75
|
+
rescue StandardError => e
|
76
|
+
puts 'error'
|
75
77
|
p e
|
76
78
|
puts e.backtrace.join("\n")
|
77
79
|
end
|
78
|
-
|
80
|
+
end
|
79
81
|
|
80
82
|
puts "pid: #{Process.pid}"
|
81
|
-
puts
|
83
|
+
puts 'Listening on port 1234...'
|
@@ -1,25 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
|
+
require 'polyphony/auto_run'
|
4
5
|
require 'polyphony/http'
|
5
6
|
|
6
7
|
opts = {
|
7
|
-
reuse_addr:
|
8
|
+
reuse_addr: true,
|
8
9
|
dont_linger: true
|
9
10
|
}
|
10
11
|
|
11
12
|
spin do
|
12
13
|
Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
|
13
|
-
req.respond(
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
req.respond("Hello world!\n")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
spin do
|
19
|
+
throttled_loop(1) do
|
20
|
+
puts "Coprocess count: #{Polyphony::Coprocess.list.size}"
|
18
21
|
end
|
19
|
-
rescue Exception => e
|
20
|
-
puts "*" * 40
|
21
|
-
p e
|
22
22
|
end
|
23
23
|
|
24
24
|
puts "pid: #{Process.pid}"
|
25
|
-
puts
|
25
|
+
puts 'Listening on port 1234...'
|
@@ -4,23 +4,24 @@ require 'bundler/setup'
|
|
4
4
|
require 'polyphony/http'
|
5
5
|
|
6
6
|
opts = {
|
7
|
-
reuse_addr:
|
8
|
-
dont_linger: true
|
7
|
+
reuse_addr: true,
|
8
|
+
dont_linger: true
|
9
9
|
}
|
10
10
|
|
11
11
|
server = Polyphony::HTTP::Server.listen('0.0.0.0', 1234, opts)
|
12
12
|
|
13
|
-
puts
|
13
|
+
puts 'Listening on port 1234'
|
14
14
|
|
15
15
|
child_pids = []
|
16
16
|
4.times do
|
17
|
-
|
17
|
+
pid = Polyphony.fork do
|
18
18
|
puts "forked pid: #{Process.pid}"
|
19
19
|
server.each do |req|
|
20
20
|
req.respond("Hello world! from pid: #{Process.pid}\n")
|
21
21
|
end
|
22
22
|
rescue Interrupt
|
23
23
|
end
|
24
|
+
child_pids << pid
|
24
25
|
end
|
25
26
|
|
26
|
-
child_pids.each { |pid| EV::Child.new(pid).await }
|
27
|
+
child_pids.each { |pid| EV::Child.new(pid).await }
|
@@ -5,11 +5,11 @@ require 'polyphony/http'
|
|
5
5
|
|
6
6
|
$throttler = throttle(1000)
|
7
7
|
opts = { reuse_addr: true, dont_linger: true }
|
8
|
-
spin
|
8
|
+
spin do
|
9
9
|
Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
|
10
10
|
$throttler.call { req.respond("Hello world!\n") }
|
11
11
|
end
|
12
|
-
|
12
|
+
end
|
13
13
|
|
14
14
|
puts "pid: #{Process.pid}"
|
15
|
-
puts
|
15
|
+
puts 'Listening on port 1234...'
|
@@ -7,12 +7,12 @@ require 'polyphony/http'
|
|
7
7
|
require 'polyphony/websocket'
|
8
8
|
|
9
9
|
def ws_handler(conn)
|
10
|
-
timer = spin
|
11
|
-
throttled_loop(1)
|
10
|
+
timer = spin do
|
11
|
+
throttled_loop(1) do
|
12
12
|
conn << Time.now.to_s
|
13
|
-
|
14
|
-
|
15
|
-
while msg = conn.recv
|
13
|
+
end
|
14
|
+
end
|
15
|
+
while (msg = conn.recv)
|
16
16
|
conn << "you said: #{msg}"
|
17
17
|
end
|
18
18
|
ensure
|
@@ -20,20 +20,20 @@ ensure
|
|
20
20
|
end
|
21
21
|
|
22
22
|
opts = {
|
23
|
-
reuse_addr:
|
23
|
+
reuse_addr: true,
|
24
24
|
dont_linger: true,
|
25
|
-
upgrade:
|
25
|
+
upgrade: {
|
26
26
|
websocket: Polyphony::Websocket.handler(&method(:ws_handler))
|
27
27
|
}
|
28
28
|
}
|
29
29
|
|
30
30
|
HTML = IO.read(File.join(__dir__, 'ws_page.html'))
|
31
31
|
|
32
|
-
spin
|
33
|
-
|
32
|
+
spin do
|
33
|
+
Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
|
34
34
|
req.respond(HTML, 'Content-Type' => 'text/html')
|
35
35
|
end
|
36
|
-
|
36
|
+
end
|
37
37
|
|
38
38
|
puts "pid: #{Process.pid}"
|
39
|
-
puts
|
39
|
+
puts 'Listening on port 1234...'
|
@@ -6,17 +6,17 @@ require 'localhost/authority'
|
|
6
6
|
|
7
7
|
authority = Localhost::Authority.fetch
|
8
8
|
opts = {
|
9
|
-
reuse_addr:
|
10
|
-
dont_linger:
|
9
|
+
reuse_addr: true,
|
10
|
+
dont_linger: true,
|
11
11
|
secure_context: authority.server_context
|
12
12
|
}
|
13
13
|
|
14
14
|
puts "pid: #{Process.pid}"
|
15
|
-
puts
|
15
|
+
puts 'Listening on port 1234...'
|
16
16
|
Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
|
17
|
-
req.respond(
|
17
|
+
req.respond("Hello world!\n")
|
18
18
|
# req.send_headers
|
19
|
-
# req.
|
20
|
-
# req.
|
21
|
-
# req.
|
22
|
-
end
|
19
|
+
# req.send_chunk("Method: #{req.method}\n")
|
20
|
+
# req.send_chunk("Path: #{req.path}\n")
|
21
|
+
# req.send_chunk("Query: #{req.query.inspect}\n", done: true)
|
22
|
+
end
|
@@ -7,12 +7,14 @@ require 'localhost/authority'
|
|
7
7
|
STDOUT.sync = true
|
8
8
|
|
9
9
|
def ws_handler(conn)
|
10
|
-
timer = spin
|
11
|
-
throttled_loop(1)
|
12
|
-
conn << Time.now.to_s
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
timer = spin do
|
11
|
+
throttled_loop(1) do
|
12
|
+
conn << Time.now.to_s
|
13
|
+
rescue StandardError
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
while (msg = conn.recv)
|
16
18
|
puts "msg: #{msg}"
|
17
19
|
# conn << "you said: #{msg}"
|
18
20
|
end
|
@@ -22,10 +24,10 @@ end
|
|
22
24
|
|
23
25
|
authority = Localhost::Authority.fetch
|
24
26
|
opts = {
|
25
|
-
reuse_addr:
|
26
|
-
dont_linger:
|
27
|
+
reuse_addr: true,
|
28
|
+
dont_linger: true,
|
27
29
|
secure_context: authority.server_context,
|
28
|
-
upgrade:
|
30
|
+
upgrade: {
|
29
31
|
websocket: Polyphony::Websocket.handler(&method(:ws_handler))
|
30
32
|
}
|
31
33
|
}
|
@@ -33,7 +35,7 @@ opts = {
|
|
33
35
|
HTML = IO.read(File.join(__dir__, 'wss_page.html'))
|
34
36
|
|
35
37
|
puts "pid: #{Process.pid}"
|
36
|
-
puts
|
38
|
+
puts 'Listening on port 1234...'
|
37
39
|
Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
|
38
40
|
req.respond(HTML, 'Content-Type' => 'text/html')
|
39
|
-
end
|
41
|
+
end
|