em-pusher-client 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b6613c790b8733c6c079eef2f2363a6b9cab073
4
- data.tar.gz: 7a8d966f51b78a1dfe666951a36ae5e9b7a5e347
3
+ metadata.gz: 4fd6883f4b562541f783fe9c959e945c2ae002cd
4
+ data.tar.gz: a50e5b95bdb7aac64c8e83e43621726c32bf3767
5
5
  SHA512:
6
- metadata.gz: 1985a4aa80bf970dc44cb817aa40408b87460ff561cbe24f9e1b4aa3839387dec6dad01dfb7b7b4f0fede179d857eb921a0d605454468ae00caaf5ab29603317
7
- data.tar.gz: 9b24b97a39619613c27f125073d972f7e92e213cb8a29d9f107685512914d08d3361982131f9bbb18dd011eaf47b8d418639cf856a18aca81f56e03a8e7a4636
6
+ metadata.gz: 7461898ed226b93ece2013c3bf1e71e9cff92eca5a115bce66761eecdaae0b397cb9db11523aa29d3806a6b09cbbcfad8c83f9ba0c1b6c0f05f9377c5a3fd851
7
+ data.tar.gz: 0cd868c86f26b9ae71f5457a576d17b48bf02d62a2f0dd855f220c32b59599042cd8f68e1adcfac013a829105dccf3a2ef53203de484fa1740f60d146ae3c39f
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './lib/em/pusher/client'
4
+
5
+ pusher_key = 'pusher_key'
6
+ channel = 'private-channel'
7
+ pusher_secret = 'pusher_secret'
8
+
9
+ EM.run do
10
+ opts = {
11
+ key: pusher_key,
12
+ cluster: 'us2',
13
+ port: 80,
14
+ scheme: 'ws',
15
+ }
16
+ EM::Pusher::Client.connect(opts) do |conn|
17
+ conn.connected do
18
+ puts 'connected'
19
+ end
20
+
21
+ conn.connection_established do |socket_id|
22
+ puts 'callback'
23
+ auth = EM::Pusher::Client.build_auth(pusher_secret, pusher_key, socket_id, channel)
24
+ msg = {
25
+ event: 'pusher:subscribe',
26
+ data: {
27
+ channel: channel,
28
+ auth: auth,
29
+ },
30
+ }
31
+ conn.send_msg(msg)
32
+ end
33
+
34
+ conn.errback do |e|
35
+ puts "Got error: #{e}"
36
+ end
37
+
38
+ conn.stream do |msg|
39
+ case msg.event
40
+ when 'pusher:connection_established'
41
+ puts 'Connection Established'
42
+ when 'pusher_internal:subscription_succeeded'
43
+ puts "Subscribed to #{msg.json['channel']}"
44
+ when 'someevent'
45
+ puts "#{msg.type}: #{msg.event} >> #{msg.data}"
46
+ else
47
+ puts "unexpected event (#{msg.type}): <#{msg}>"
48
+ end
49
+ # conn.close_connection if closed?
50
+ end
51
+
52
+ conn.disconnect do
53
+ puts 'gone'
54
+ EM.stop_event_loop
55
+ end
56
+ end
57
+ end
@@ -27,6 +27,17 @@ module EM
27
27
  end
28
28
  end
29
29
 
30
+ def self.sign(secret, socket_id, channel_name)
31
+ digest = OpenSSL::Digest::SHA256.new
32
+ string_to_sign = "#{socket_id}:#{channel_name}"
33
+ OpenSSL::HMAC.hexdigest(digest, secret, string_to_sign)
34
+ end
35
+
36
+ def self.build_auth(secret, key, socket_id, channel_name)
37
+ sig = sign(secret, socket_id, channel_name)
38
+ "#{key}:#{sig}"
39
+ end
40
+
30
41
  def self.url(options)
31
42
  opts = DEFAULT_OPTIONS.merge(options)
32
43
  REQUIRED_OPTIONS.each { |opt| fail ArgumentError, "option #{opt} is required" unless opts[opt] }
@@ -4,6 +4,7 @@ require 'eventmachine'
4
4
  require 'uri'
5
5
  require 'json'
6
6
  require 'websocket'
7
+ require 'openssl'
7
8
 
8
9
  module EM
9
10
  module Pusher
@@ -53,6 +54,14 @@ module EM
53
54
  @disconnect = cback
54
55
  end
55
56
 
57
+ def connection_established(&cback)
58
+ @connection_established = cback
59
+ end
60
+
61
+ def ping(&cback)
62
+ @ping = cback
63
+ end
64
+
56
65
  # https://pusher.com/docs/pusher_protocol#subscription-events
57
66
  def subscribe(channel, auth = nil, channel_data = nil)
58
67
  msg = {
@@ -98,8 +107,34 @@ module EM
98
107
  def handle_received_data(data)
99
108
  @frame << data
100
109
  while (msg = @frame.next)
101
- @stream.call(EM::Pusher::Client::MsgParser.new(msg)) if @stream
110
+ process_data(EM::Pusher::Client::MsgParser.new(msg))
111
+ end
112
+ end
113
+
114
+ def process_data(parser)
115
+ if parser.event == 'pusher:connection_established'
116
+ process_connection(parser)
117
+ elsif parser.type == :ping
118
+ process_ping
119
+ elsif parser.type == :close
120
+ process_close
102
121
  end
122
+ @stream.call(parser) if @stream
123
+ end
124
+
125
+ def process_connection(parser)
126
+ socket_id = JSON.parse(parser.json['data'])['socket_id']
127
+ @connection_established.yield(socket_id) if @connection_established
128
+ end
129
+
130
+ def process_ping
131
+ @ping.yield if @ping
132
+ send_msg(event: 'pusher:pong')
133
+ end
134
+
135
+ def process_close
136
+ unbind
137
+ close_connection
103
138
  end
104
139
  end
105
140
  end
@@ -4,14 +4,12 @@ module EM
4
4
  module Pusher
5
5
  module Client
6
6
  class MsgParser
7
- attr_reader :json,
8
- :data
9
-
10
7
  def initialize(msg)
11
- parse(msg)
8
+ @msg = msg
12
9
  end
13
10
 
14
11
  def event
12
+ return '{}' unless json
15
13
  @event ||= json['event']
16
14
  end
17
15
 
@@ -19,20 +17,25 @@ module EM
19
17
  @msg.to_s
20
18
  end
21
19
 
22
- private
20
+ def type
21
+ @msg.type
22
+ end
23
+
24
+ def json
25
+ @json ||= JSON.parse(@msg.data)
26
+ rescue JSON::ParserError
27
+ {}
28
+ end
23
29
 
24
- def parse(msg)
25
- @msg = msg
26
- @json = JSON.parse(msg.data)
30
+ def data
27
31
  @data =
28
32
  if json['data'].is_a?(String)
29
33
  JSON.parse(json['data'])
30
34
  else
31
35
  json['data']
32
36
  end
33
- rescue JSON::ParserError => e
34
- puts "Error parsing event '#{event}': '#{e.message}'"
35
- # logger.error("Error parsing msg #{e}")
37
+ rescue JSON::ParserError
38
+ {}
36
39
  end
37
40
  end
38
41
  end
@@ -3,7 +3,7 @@
3
3
  module Em
4
4
  module Pusher
5
5
  module Client
6
- VERSION = '0.1.1'
6
+ VERSION = '0.1.2'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-pusher-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genaro Madrid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-26 00:00:00.000000000 Z
11
+ date: 2018-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -174,6 +174,7 @@ files:
174
174
  - em-pusher-client.gemspec
175
175
  - em-pusher-client.sublime-project
176
176
  - examples/normal.rb
177
+ - examples/private.rb
177
178
  - lib/em/pusher/client.rb
178
179
  - lib/em/pusher/client/connection.rb
179
180
  - lib/em/pusher/client/msg_parser.rb