-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.rb
More file actions
44 lines (37 loc) · 990 Bytes
/
app.rb
File metadata and controls
44 lines (37 loc) · 990 Bytes
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
require 'sinatra/base'
require 'faye'
require 'faye/redis'
require 'thin'
require 'json'
EM.run do
# Start Faye Server
Faye::RackAdapter.new(
:mount => '/faye',
:timeout => 25,
:engine => {
:type => Faye::Redis,
:host => '127.0.0.1',
:port => 6789,
:namespace => 'faye'
}
).listen(3001)
# Connect to Faye Server
$client = Faye::Client.new('http://example.com:3001/faye')
# Subscribe to client messages
$client.subscribe('/fromclient') do |message|
puts "> %s" % message
$client.publish '/fromserver', "Received message length: #{message.length}"
end
# Simple app to show how to post messages from the server side
class App < Sinatra::Base
post '/' do
msg = JSON.parse(request.body.read)['message']
puts "< %s" % msg
$client.publish '/fromserver', "You asked me to send: #{msg}"
end
get '/' do
redirect '/index.html'
end
end
Thin::Server.start App, '0.0.0.0', 3000
end