From 8625518725575de5524405b264d901b691fce066 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Tue, 15 Dec 2015 10:35:02 -0500 Subject: Bring latest changes over from actioncable/master --- actioncable/README.md | 6 +++--- actioncable/lib/assets/javascripts/cable.coffee.erb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'actioncable') diff --git a/actioncable/README.md b/actioncable/README.md index 1781e3e82e..aa771f52c0 100644 --- a/actioncable/README.md +++ b/actioncable/README.md @@ -91,7 +91,7 @@ The client-side needs to setup a consumer instance of this connection. That's do #= require cable @App = {} -App.cable = Cable.createConsumer "ws://cable.example.com" +App.cable = Cable.createConsumer("ws://cable.example.com") ``` The ws://cable.example.com address must point to your set of Action Cable servers, and it @@ -402,7 +402,7 @@ bundle exec puma -p 28080 cable/config.ru ``` The above will start a cable server on port 28080. Remember to point your client-side setup against that using something like: -`App.cable.createConsumer('ws://basecamp.dev:28080')`. +`App.cable = Cable.createConsumer("ws://basecamp.dev:28080")`. ### In app @@ -415,7 +415,7 @@ Example::Application.routes.draw do end ``` -You can use `App.cable.createConsumer('ws://' + window.location.host + '/websocket')` to connect to the cable server. +You can use `App.cable = Cable.createConsumer("/websocket")` to connect to the cable server. For every instance of your server you create and for every worker your server spawns, you will also have a new instance of ActionCable, but the use of Redis keeps messages synced across connections. diff --git a/actioncable/lib/assets/javascripts/cable.coffee.erb b/actioncable/lib/assets/javascripts/cable.coffee.erb index 25a9fc79c2..a722f27ac1 100644 --- a/actioncable/lib/assets/javascripts/cable.coffee.erb +++ b/actioncable/lib/assets/javascripts/cable.coffee.erb @@ -5,8 +5,19 @@ INTERNAL: <%= ActionCable::INTERNAL.to_json %> createConsumer: (url = @getConfig("url")) -> - new Cable.Consumer url + new Cable.Consumer @createWebSocketURL(url) getConfig: (name) -> element = document.head.querySelector("meta[name='action-cable-#{name}']") element?.getAttribute("content") + + createWebSocketURL: (url) -> + if url and not /^wss?:/i.test(url) + a = document.createElement("a") + a.href = url + # Fix populating Location properties in IE. Otherwise, protocol will be blank. + a.href = a.href + a.protocol = a.protocol.replace("http", "ws") + a.href + else + url -- cgit v1.2.3