Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created October 3, 2012 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoglan/3829094 to your computer and use it in GitHub Desktop.
Save jcoglan/3829094 to your computer and use it in GitHub Desktop.
# Run this program with `ruby`. It will print a port number, e.g. `[:port, 56789]`. Take
# that number and open a WebSocket to it in Chrome and Firefox:
#
# new WebSocket('ws://localhost:56789/')
#
# For the Chrome socket, the program prints the HTTP handshake headers from the socket
# then exists. For Firefox it prints nothing. Why? And what should I be doing differently
# to read data from it?
#
# This EventMachine TCP server receives data from Firefox correctly:
#
# require 'eventmachine'
#
# module Connection
# def receive_data(data)
# puts data
# end
# end
#
# EM.run {
# EM.start_server('0.0.0.0', 80, Connection)
# }
#
# Here are the Wireshark captures of what each browser is sending:
=begin
Chrome:
0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 ........ ......E.
0010 01 1f e2 37 40 00 40 06 59 9f 7f 00 00 01 7f 00 ...7@.@. Y.......
0020 00 01 c3 19 00 50 fb 39 60 4b 1f d5 f3 eb 80 18 .....P.9 `K......
0030 01 01 ff 13 00 00 01 01 08 0a 07 81 82 7f 07 81 ........ ........
0040 82 7f 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 ..GET / HTTP/1.1
0050 0d 0a 55 70 67 72 61 64 65 3a 20 77 65 62 73 6f ..Upgrad e: webso
0060 63 6b 65 74 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e cket..Co nnection
0070 3a 20 55 70 67 72 61 64 65 0d 0a 48 6f 73 74 3a : Upgrad e..Host:
0080 20 6c 6f 63 61 6c 68 6f 73 74 0d 0a 4f 72 69 67 localho st..Orig
0090 69 6e 3a 20 68 74 74 70 3a 2f 2f 6e 65 77 73 2e in: http ://news.
00a0 79 63 6f 6d 62 69 6e 61 74 6f 72 2e 63 6f 6d 0d ycombina tor.com.
00b0 0a 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 4b .Sec-Web Socket-K
00c0 65 79 3a 20 64 38 4d 61 7a 6b 37 7a 65 41 50 4e ey: d8Ma zk7zeAPN
00d0 48 31 4e 6c 49 75 4b 71 72 41 3d 3d 0d 0a 53 65 H1NlIuKq rA==..Se
00e0 63 2d 57 65 62 53 6f 63 6b 65 74 2d 56 65 72 73 c-WebSoc ket-Vers
00f0 69 6f 6e 3a 20 31 33 0d 0a 53 65 63 2d 57 65 62 ion: 13. .Sec-Web
0100 53 6f 63 6b 65 74 2d 45 78 74 65 6e 73 69 6f 6e Socket-E xtension
0110 73 3a 20 78 2d 77 65 62 6b 69 74 2d 64 65 66 6c s: x-web kit-defl
0120 61 74 65 2d 66 72 61 6d 65 0d 0a 0d 0a ate-fram e....
Firefox:
0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 ........ ......E.
0010 02 04 76 c3 40 00 40 06 c4 2e 7f 00 00 01 7f 00 ..v.@.@. ........
0020 00 01 c3 1c 00 50 2d 96 ff 63 28 d5 ac 19 80 18 .....P-. .c(.....
0030 01 01 ff f8 00 00 01 01 08 0a 07 81 89 a3 07 81 ........ ........
0040 89 a3 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 ..GET / HTTP/1.1
0050 0d 0a 48 6f 73 74 3a 20 6c 6f 63 61 6c 68 6f 73 ..Host: localhos
0060 74 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 4d t..User- Agent: M
0070 6f 7a 69 6c 6c 61 2f 35 2e 30 20 28 58 31 31 3b ozilla/5 .0 (X11;
0080 20 55 62 75 6e 74 75 3b 20 4c 69 6e 75 78 20 78 Ubuntu; Linux x
0090 38 36 5f 36 34 3b 20 72 76 3a 31 35 2e 30 29 20 86_64; r v:15.0)
00a0 47 65 63 6b 6f 2f 32 30 31 30 30 31 30 31 20 46 Gecko/20 100101 F
00b0 69 72 65 66 6f 78 2f 31 35 2e 30 2e 31 0d 0a 41 irefox/1 5.0.1..A
00c0 63 63 65 70 74 3a 20 74 65 78 74 2f 68 74 6d 6c ccept: t ext/html
00d0 2c 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 68 74 ,applica tion/xht
00e0 6d 6c 2b 78 6d 6c 2c 61 70 70 6c 69 63 61 74 69 ml+xml,a pplicati
00f0 6f 6e 2f 78 6d 6c 3b 71 3d 30 2e 39 2c 2a 2f 2a on/xml;q =0.9,*/*
0100 3b 71 3d 30 2e 38 0d 0a 41 63 63 65 70 74 2d 4c ;q=0.8.. Accept-L
0110 61 6e 67 75 61 67 65 3a 20 65 6e 2d 67 62 2c 65 anguage: en-gb,e
0120 6e 3b 71 3d 30 2e 35 0d 0a 41 63 63 65 70 74 2d n;q=0.5. .Accept-
0130 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 2c 20 Encoding : gzip,
0140 64 65 66 6c 61 74 65 0d 0a 43 6f 6e 6e 65 63 74 deflate. .Connect
0150 69 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 76 65 2c ion: kee p-alive,
0160 20 55 70 67 72 61 64 65 0d 0a 53 65 63 2d 57 65 Upgrade ..Sec-We
0170 62 53 6f 63 6b 65 74 2d 56 65 72 73 69 6f 6e 3a bSocket- Version:
0180 20 31 33 0d 0a 4f 72 69 67 69 6e 3a 20 68 74 74 13..Ori gin: htt
0190 70 3a 2f 2f 6c 6f 63 61 6c 2e 64 65 76 3a 37 30 p://loca l.dev:70
01a0 30 34 0d 0a 53 65 63 2d 57 65 62 53 6f 63 6b 65 04..Sec- WebSocke
01b0 74 2d 4b 65 79 3a 20 66 70 71 66 34 7a 72 71 59 t-Key: f pqf4zrqY
01c0 63 63 2f 6c 2b 58 33 6e 71 36 44 48 77 3d 3d 0d cc/l+X3n q6DHw==.
01d0 0a 50 72 61 67 6d 61 3a 20 6e 6f 2d 63 61 63 68 .Pragma: no-cach
01e0 65 0d 0a 43 61 63 68 65 2d 43 6f 6e 74 72 6f 6c e..Cache -Control
01f0 3a 20 6e 6f 2d 63 61 63 68 65 0d 0a 55 70 67 72 : no-cac he..Upgr
0200 61 64 65 3a 20 77 65 62 73 6f 63 6b 65 74 0d 0a ade: web socket..
0210 0d 0a ..
=end
require 'socket'
server = TCPServer.open(0)
p [:port, server.addr[1]]
socket = server.accept
while msg = socket.gets
puts msg
break if msg == "\r\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment