-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
543 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,4 @@ Gemfile.lock | |
|
||
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: | ||
.rvmrc | ||
*.sublime-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: ruby | ||
sudo: false | ||
rvm: | ||
- 2.1 | ||
- 2.2 | ||
- 2.3.0 | ||
- 2.5.0 | ||
notifications: | ||
email: | ||
on_success: change | ||
on_failure: always | ||
script: | ||
- bundle exec rubocop | ||
- bundle exec rspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,7 @@ | |
|
||
require 'bundler/gem_tasks' | ||
task default: :spec | ||
|
||
task :console do | ||
exec 'pry -r em/pusher/client -I ./lib' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require './lib/em/pusher/client' | ||
|
||
EM.run do | ||
opts = { | ||
key: 'my-key', | ||
cluster: 'us2', | ||
port: 80, | ||
scheme: 'ws', | ||
} | ||
EM::Pusher::Client.connect(opts) do |conn| | ||
conn.connected do | ||
puts 'connected' | ||
end | ||
|
||
conn.callback do | ||
puts 'callback' | ||
msg = { | ||
event: 'pusher:subscribe', | ||
data: { | ||
channel: 'my-channel', | ||
}, | ||
} | ||
conn.send_msg(msg) | ||
end | ||
|
||
conn.errback do |e| | ||
puts "Got error: #{e}" | ||
end | ||
|
||
conn.stream do |msg| | ||
puts "stream: <#{msg}>" | ||
case msg.event | ||
when 'pusher:connection_established' | ||
puts 'Connection Established' | ||
when 'pusher_internal:subscription_succeeded' | ||
puts "Subscribed to #{msg.json['channel']}" | ||
when 'someevent' | ||
puts "someevent: #{msg.data}" | ||
end | ||
# conn.close_connection if closed? | ||
end | ||
|
||
conn.disconnect do | ||
puts 'gone' | ||
EM.stop_event_loop | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'em/pusher/client/version' | ||
require_relative 'client/version' | ||
require_relative 'client/connection' | ||
require_relative 'client/msg_parser' | ||
|
||
module Em | ||
module EM | ||
module Pusher | ||
module Client | ||
# Your code goes here... | ||
DEFAULT_OPTIONS = { | ||
app_id: nil, | ||
app_secret: nil, | ||
scheme: 'ws', | ||
port: 80, | ||
encrypted: 'on', | ||
protocol: 4, | ||
version: '4.2', | ||
client_name: 'em-pusher-client', | ||
}.freeze | ||
|
||
REQUIRED_OPTIONS = %i[key cluster].freeze | ||
|
||
def self.connect(options) | ||
uri = url(options) | ||
Connection.connect(uri).tap do |conn| | ||
yield conn if block_given? | ||
end | ||
end | ||
|
||
def self.url(options) | ||
opts = DEFAULT_OPTIONS.merge(options) | ||
REQUIRED_OPTIONS.each { |opt| fail ArgumentError, "option #{opt} is required" unless opts[opt] } | ||
"#{opts[:scheme]}://ws-#{opts[:cluster]}.pusher.com:#{opts[:port]}/app/#{opts[:key]}" \ | ||
"?protocol=#{opts[:protocol]}" \ | ||
"&client=#{opts[:client_name]}" \ | ||
"&version=#{opts[:version]}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'eventmachine' | ||
require 'uri' | ||
require 'json' | ||
require 'websocket' | ||
|
||
module EM | ||
module Pusher | ||
module Client | ||
class Connection < EM::Connection | ||
include EM::Deferrable | ||
|
||
attr_accessor :url | ||
attr_accessor :protocol_version | ||
attr_accessor :origin | ||
|
||
def self.connect(uri, opts = {}) | ||
p_uri = URI.parse(uri) | ||
conn = EM.connect(p_uri.host, p_uri.port || 80, self) do |c| | ||
c.url = uri | ||
c.protocol_version = opts[:version] | ||
c.origin = opts[:origin] | ||
end | ||
yield conn if block_given? | ||
conn | ||
end | ||
|
||
def post_init | ||
@handshaked = false | ||
@frame = ::WebSocket::Frame::Incoming::Client.new | ||
end | ||
|
||
def connection_completed | ||
@connect.yield if @connect | ||
@hs = ::WebSocket::Handshake::Client.new( | ||
url: @url, | ||
origin: @origin, | ||
version: @protocol_version, | ||
) | ||
send_data(@hs.to_s) | ||
end | ||
|
||
def stream(&cback) | ||
@stream = cback | ||
end | ||
|
||
def connected(&cback) | ||
@connect = cback | ||
end | ||
|
||
def disconnect(&cback) | ||
@disconnect = cback | ||
end | ||
|
||
# https://pusher.com/docs/pusher_protocol#subscription-events | ||
def subscribe(channel, auth = nil, channel_data = nil) | ||
msg = { | ||
event: 'pusher:subscribe', | ||
data: { | ||
channel: channel, | ||
auth: auth, | ||
channel_data: channel_data, | ||
}, | ||
} | ||
conn.send_msg(msg) | ||
end | ||
|
||
def receive_data(data) | ||
return handle_received_data(data) if @handshaked | ||
@hs << data | ||
if @hs.finished? | ||
@handshaked = true | ||
succeed | ||
end | ||
|
||
receive_data(@hs.leftovers) if @hs.leftovers | ||
end | ||
|
||
def send_msg(data, args = {}) | ||
type = args[:type] || :text | ||
data = data.to_json if data.is_a?(Hash) | ||
frame = ::WebSocket::Frame::Outgoing::Client.new( | ||
data: data, | ||
type: type, | ||
version: @hs.version, | ||
) | ||
send_data(frame.to_s) | ||
end | ||
|
||
def unbind | ||
super | ||
@disconnect.call if @disconnect | ||
end | ||
|
||
private | ||
|
||
def handle_received_data(data) | ||
@frame << data | ||
while (msg = @frame.next) | ||
@stream.call(EM::Pusher::Client::MsgParser.new(msg)) if @stream | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.