-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
12 changed files
with
228 additions
and
12 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
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
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "action_cable" | ||
|
||
ActionCable::Server::Base.prepend(Module.new do | ||
def broadcast(channel, payload, **options) | ||
return super if options.empty? | ||
|
||
AnyCable::Rails.with_broadcast_options(**options) do | ||
super(channel, payload) | ||
end | ||
end | ||
end) | ||
|
||
ActionCable::Channel::Base.singleton_class.prepend(Module.new do | ||
def broadcast_to(target, payload, **options) | ||
return super if options.empty? | ||
|
||
AnyCable::Rails.with_broadcast_options(**options) do | ||
super(target, payload) | ||
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
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module AnyCable | ||
module Rails | ||
module SocketIdTracking | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around_action :anycable_tracking_socket_id | ||
end | ||
|
||
private | ||
|
||
def anycable_tracking_socket_id(&block) | ||
Rails.with_socket_id(request.headers[AnyCable.config.socket_id_header], &block) | ||
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
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require "action_controller/test_case" | ||
|
||
supported = AnyCable.method(:broadcast).arity != 2 | ||
|
||
describe "broadcast to others", skip: !supported do | ||
include ActionDispatch::Integration::Runner | ||
include ActionDispatch::IntegrationTest::Behavior | ||
|
||
# Delegates to `Rails.application`. | ||
def app | ||
::Rails.application | ||
end | ||
|
||
before { allow(AnyCable.broadcast_adapter).to receive(:raw_broadcast) } | ||
|
||
it "adds exclude_socket meta if X-Socket-ID header is provided" do | ||
post "/broadcasts", params: {count: 1, to_others: true, disable_auto_batching: true}, headers: {"X-Socket-ID" => "134"} | ||
|
||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).once | ||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).with( | ||
{ | ||
stream: "test", | ||
data: {count: 1}.to_json, | ||
meta: {exclude_socket: "134"} | ||
}.to_json | ||
) | ||
end | ||
|
||
it "doesn't add meta if header is missing" do | ||
post "/broadcasts", params: {count: 1, to_others: true, disable_auto_batching: true} | ||
|
||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).once | ||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).with( | ||
{ | ||
stream: "test", | ||
data: {count: 1}.to_json | ||
}.to_json | ||
) | ||
end | ||
|
||
context "with custom header" do | ||
around do |example| | ||
was_value = AnyCable.config.socket_id_header | ||
AnyCable.config.socket_id_header = "X-My-Socket-ID" | ||
example.run | ||
ensure | ||
AnyCable.config.socket_id_header = was_value | ||
end | ||
|
||
specify do | ||
post "/broadcasts", params: {count: 1, to_others: true, disable_auto_batching: true}, headers: {"X-My-Socket-ID" => "134"} | ||
|
||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).once | ||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).with( | ||
{ | ||
stream: "test", | ||
data: {count: 1}.to_json, | ||
meta: {exclude_socket: "134"} | ||
}.to_json | ||
) | ||
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
supported = AnyCable.method(:broadcast).arity != 2 | ||
|
||
describe ActionCable::Channel::Base, skip: !supported do | ||
before { allow(AnyCable.broadcast_adapter).to receive(:raw_broadcast) } | ||
|
||
describe ".broadcast_to" do | ||
context "with to_others: true" do | ||
it "adds exclude_socket meta if current_socket_id is defined" do | ||
AnyCable::Rails.with_socket_id("456") do | ||
TestChannel.broadcast_to("test", {foo: "bar"}, to_others: true) | ||
end | ||
|
||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).once | ||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).with( | ||
{ | ||
stream: "test:test", | ||
data: {foo: "bar"}.to_json, | ||
meta: {exclude_socket: "456"} | ||
}.to_json | ||
) | ||
end | ||
end | ||
|
||
context "when broadcasting_to_others is set" do | ||
specify do | ||
AnyCable::Rails.with_socket_id("456") do | ||
AnyCable::Rails.broadcasting_to_others do | ||
TestChannel.broadcast_to("test", {foo: "baz"}) | ||
end | ||
end | ||
|
||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).once | ||
expect(AnyCable.broadcast_adapter).to have_received(:raw_broadcast).with( | ||
{ | ||
stream: "test:test", | ||
data: {foo: "baz"}.to_json, | ||
meta: {exclude_socket: "456"} | ||
}.to_json | ||
) | ||
end | ||
end | ||
end | ||
end |