EDIT: At the end, it shows that update headers have been created.
I am working from action-cable-example codebase trying to create a WebSocket application. The Chatty application, which depends on the browser client provided in the application, works great. But I will not use this client as I need an external IoT connection. As a result, I try to implement WebSocket ws / wss protocols for external non-browsers, and my connection is in route.rb:
mount ActionCable.server => '/cable'
I tried several external clients, such as the Chrome Simple WebSocket client extension and gem websocket-client-simple using sample / client.rb. In both cases, the ActionCable does not return update headers. The Chrome extension complains as follows:
WebSocket connection to 'ws://127.0.0.1:3000/cable' failed: Error during WebSocket handshake: 'Upgrade' header is missing
The actual handshake shows that this is true, as in:
**General**
Request URL:ws://127.0.0.1:3000/cable
Request Method:GET
Status Code:101 Switching Protocols
**Response Headers**
view source
Connection:keep-alive
Server:thin
**Request Headers**
view source
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Cookie:PPA_ID=<redacted>
DNT:1
Host:127.0.0.1:3000
Origin:chrome-extension://pfdhoblngboilpfeibdedpjgfnlcodoo
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:1vokmzewcWf9e2RwMth0Lw==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
According to the standards, the response headers should be:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Accept is especially important because it is computed based on the Sec-WebSocket-Key request header to confirm that ws / wss is understood and that there should be switching protocols.
During all this, the server is happier until the client marks and closes the connection:
Started GET "/cable" for 127.0.0.1 at 2016-06-16 19:19:17 -0400
ActiveRecord::SchemaMigration Load (1.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2016-06-16 19:19:17 -0400
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2016-06-16 19:19:18 -0400
Looking at websocket-client-simple, I broke the WebSocket returned by client.rb and it also showed empty headers. I show the code and then WebSocket:
url = ARGV.shift || 'ws://localhost:3000/cable'
ws = WebSocket::Client::Simple.connect url
@url="ws://localhost:3000/cable",
@socket=#<TCPSocket:fd 3>,
@handshake=<WebSocket::Handshake::Client:0x013231c8
@url="ws://localhost:3000/cable",
@headers={},
@state=:new,
@handler=#<WebSocket::Handshake::Handler::Client11:0x2e88400
@handshake=<WebSocket::Handshake::Client:0x013231c8
@url="ws://localhost:3000/cable",
@headers={},
@state=:new,
@handler=#<WebSocket::Handshake::Handler::Client11:0x2e88400 ...>,
@data="",
@secure=false,
@host="localhost",
@port=3000,
@path="/cable",
@query=nil,
@version=13>,
@key="KUJ0/C0rvoCMruW8STp0Sw==">,
@data="",
@secure=false,
@host="localhost",
@port=3000,
@path="/cable",
@query=nil,
@version=13>,
@handshaked=false,
@pipe_broken=false,
@closed=false,
@__events=[{:type=>:__close, :listener=>
@thread=#<Thread:0x2d10a70@D:/Bitnami/rubystack-2.2.5-3/projects/websocket-client-simple/lib/websocket-client-simple/client.rb:42 sleep>
>;
, "@handshaked" false. , , .
UPDATE:
, WebSocket:: Driver.start . , @socket.write(response) EventMachine.
:
def start
return false unless @ready_state == 0
response = handshake_response
return false unless response
@socket.write(response)
open unless @stage == -1
true
end
handshake_response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: iJVnsG1ApNMFzABXGDSHN1V0i/s=