This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
/
slanger_helper_methods.rb
96 lines (77 loc) · 2.43 KB
/
slanger_helper_methods.rb
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module SlangerHelperMethods
def start_slanger_with_options options={}
# Fork service. Our integration tests MUST block the main thread because we want to wait for i/o to finish.
@server_pid = EM.fork_reactor do
require File.expand_path(File.dirname(__FILE__) + '/../slanger.rb')
Thin::Logging.silent = true
opts = { host: '0.0.0.0',
api_port: '4567',
websocket_port: '8080',
app_key: '765ec374ae0a69f4ce44',
secret: 'your-pusher-secret' }
Slanger::Config.load opts.merge(options)
Slanger::Service.run
end
wait_for_slanger
end
alias start_slanger start_slanger_with_options
def stop_slanger
# Ensure Slanger is properly stopped. No orphaned processes allowed!
Process.kill 'SIGKILL', @server_pid
Process.wait @server_pid
end
def wait_for_slanger opts = {}
opts = { port: 8080 }.update opts
begin
TCPSocket.new('0.0.0.0', opts[:port]).close
rescue
sleep 0.005
retry
end
end
def new_websocket opts = {}
opts = { key: Pusher.key }.update opts
uri = "ws://0.0.0.0:8080/app/#{opts[:key]}?client=js&version=1.8.5"
EM::HttpRequest.new(uri).get.tap { |ws| ws.errback &errback }
end
def em_stream opts = {}
messages = []
em_thread do
websocket = new_websocket opts
stream(websocket, messages) do |message|
yield websocket, messages
end
end
return messages
end
def em_thread
Thread.new do
EM.synchrony do
yield
end
end.join
end
def stream websocket, messages
websocket.stream do |message|
messages << JSON.parse(message)
yield message
end
end
def auth_from options
id = options[:message]['data']['socket_id']
name = options[:name]
user_id = options[:user_id]
Pusher['presence-channel'].authenticate(id, {user_id: user_id, user_info: {name: name}})
end
def send_subscribe options
auth = auth_from options
options[:user].send({event: 'pusher:subscribe',
data: {channel: 'presence-channel'}.merge(auth)}.to_json)
end
def private_channel websocket, message
auth = Pusher['private-channel'].authenticate(message['data']['socket_id'])[:auth]
websocket.send({ event: 'pusher:subscribe',
data: { channel: 'private-channel',
auth: auth } }.to_json)
end
end