forked from ttanimichi/async-websocket-pubsub-example
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
47 lines (34 loc) · 1020 Bytes
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env -S falcon serve --bind http://localhost:7070 --count 1 -c
require 'async/websocket/adapters/rack'
require 'async/redis'
class ChatServer
def initialize(app, endpoint: Async::Redis.local_endpoint)
@app = app
@endpoint = endpoint
@client = Async::Redis::Client.new(endpoint)
end
def call(env)
Async::WebSocket::Adapters::Rack.open(env, protocols: ['ws']) do |connection|
loop do
channel = connection.read.to_str
subscribe(channel) do |context|
Console.info(connection, "Subscribed", channel: channel)
loop do
event = context.listen
Protocol::WebSocket::TextMessage.generate(event).send(connection)
connection.flush
end
end
end
end or @app.call(env)
end
private
def subscribe(channel, &block)
@client.subscribe(channel, &block)
end
end
use ChatServer
use Rack::Static, urls: ["/"], root: "public", index: "index.html"
run do |env|
[404, {}, []]
end