WebSocket handshake with Ruby and EM::WebSocket::Server
NickName:Chad Johnson Ask DateTime:2013-06-29T06:14:34

WebSocket handshake with Ruby and EM::WebSocket::Server

I am trying to create a simple WebSocket connection in JavaScript against my Rails app. I get the following:

WebSocket connection to 'ws://localhost:4000/' failed: Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing

What am I doing wrong? Here is my code:

JavaScript:

var socket = new WebSocket('ws://localhost:4000');

socket.onopen = function() {
  var handshake =
    "GET / HTTP/1.1\n" +
    "Host: localhost\n" +
    "Upgrade: websocket\n" +
    "Connection: Upgrade\n" +
    "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\n" +
    "Sec-WebSocket-Protocol: quote\n" +
    "Sec-WebSocket-Version: 13\n" +
    "Origin: http://localhost\n";

  socket.send(handshake);
};

socket.onmessage = function(data) {
  console.log(data);
};

Ruby:

require 'rubygems'
require 'em-websocket-server'

module QuoteService
  class WebSocket < EventMachine::WebSocket::Server
    def on_connect
      handshake_response =  "HTTP/1.1 101 Switching Protocols\n"
      handshake_response << "Upgrade: websocket\n"
      handshake_response << "Connection: Upgrade\n"
      handshake_response << "Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\n"
      handshake_response << "Sec-WebSocket-Protocol: quote\n"

      send_message(handshake_response)
    end

    def on_receive(data)
      puts 'RECEIVED: ' + data
    end
  end
end

EventMachine.run do
  print 'Starting WebSocket server...'
  EventMachine.start_server '0.0.0.0', 4000, QuoteService::WebSocket
  puts 'running'
end

The handshake headers are per Wikipedia.

Copyright Notice:Content Author:「Chad Johnson」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17374453/websocket-handshake-with-ruby-and-emwebsocketserver

More about “WebSocket handshake with Ruby and EM::WebSocket::Server” related questions

WebSocket handshake with Ruby and EM::WebSocket::Server

I am trying to create a simple WebSocket connection in JavaScript against my Rails app. I get the following: WebSocket connection to 'ws://localhost:4000/' failed: Error during WebSocket handsha...

Show Detail

WebSocket handshake with Ruby and EM::WebSocket::Server

I am trying to create a simple WebSocket connection in JavaScript against my Rails app. I get the following: WebSocket connection to 'ws://localhost:4000/' failed: Error during WebSocket handsha...

Show Detail

websocket handshake

I'm writing a websocket server using c++,I was pending in the handshake use chrome 17 as the client. When the server send client the handshake response chrome always show the error below in consol...

Show Detail

Websocket handshake manipulation

In order to create a websocket connection in javascript we can use the websocket api for example: const socket = new WebSocket('ws://localhost:8080'); then the browser will initiate an handshake ...

Show Detail

WebSocket - Closing Handshake Gorilla

Snippet from WebSocket RFC: To Start the WebSocket Closing Handshake with a status code (Section 7.4) /code/ and an optional close reason (Section 7.1.6) /reason/, an endpoint MUST send a Close

Show Detail

is Ruby em-websocket blocking?

I'm writing a ruby program that has 2 threads. One that listens on an incoming UDP connection and another that broadcasts on a websocket from which browsers on the client side read.I'm using the em-

Show Detail

Ruby server side Websocket client

Lately i have been experimenting with ruby and websockets. So i created a new Rails 5 project with ActionCable, all seems to work fine with it. But also i created a ruby plain script with the Faye's

Show Detail

How to do a Websocket handshake

I want to do handshake with server using websockets. But unnfortunately i always get errors like WebSocket connection to 'ws://localhost:80/myserverfile.php' failed: Error during WebSocket han...

Show Detail

Websocket header to reject handshake

What is the way to reject a websocket handshake. Based on the documentation to accept a handshake a response from the http server is sent like this HTTP/1.1 101 Switching Protocols Upgrade: websocket

Show Detail

Websocket handshake unable to make connection

The request fetching in on wensocket server is : Upgrade: websocket Connection: Upgrade Host: 10.1.5.20:5555 Origin: http://localhost:8080 Sec-WebSocket-Protocol: sip Pragma: no-cache Cache-Contro...

Show Detail