-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsockjs_apps_for_sockjs_protocol_tests.rb
executable file
·99 lines (80 loc) · 2.28 KB
/
sockjs_apps_for_sockjs_protocol_tests.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env ruby
# encoding: utf-8
require "bundler"
require "bundler/setup"
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
require "rack"
require "thin"
require "eventmachine"
$DEBUG = true
def next_argument(argument)
if ARGV.include?(argument)
ARGV[ARGV.index(argument) + 1]
end
end
PORT = next_argument("-p") || 8081
::Thin::Connection.class_eval do
def handle_error(error = $!)
log "[#{error.class}] #{error.message}\n - "
log error.backtrace.join("\n - ")
close_connection rescue nil
end
end
# Let's make Lint to STFU. See
# XHRSendPost#handle for an explanation.
class Rack::Lint
def call(env)
@app.call(env)
end
end
require "rack/sockjs"
require "json"
SockJS.debug!
class MyHelloWorld
def call(env)
body = <<-HTML
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>
This is the app, not SockJS.
</p>
</body>
</html>
HTML
[200, {"Content-Type" => "text/html; charset=UTF-8", "Content-Length" => body.bytesize.to_s}, [body]]
end
end
SockJS.debug "Available handlers: #{::SockJS::Transport.subclasses.inspect}"
options = {sockjs_url: "http://cdn.sockjs.org/sockjs-#{SockJS::PROTOCOL_VERSION}.min.js"}
app = Rack::Builder.new do
use Rack::SockJS, "/echo", options do |connection|
connection.subscribe do |session, message|
SockJS.debug "\033[0;31;40m[Echo]\033[0m message: #{message.inspect}, session: #{session.object_id}"
session.send(message)
end
end
use Rack::SockJS, "/disabled_websocket_echo", options.merge(websocket: false) do |connection|
connection.subscribe do |session, message|
SockJS.debug "\033[0;31;40m[Echo]\033[0m message: #{message.inspect}, session: #{session.object_id}"
session.send(message)
end
end
use Rack::SockJS, "/close", options do |connection|
# With WebSockets this occurs immediately, so the
# client receives "o" and then "c[3000, "Go away!"]".
# However with polling, this will occur with the next request.
connection.session_open do |session, message|
SockJS.debug "\033[0;31;40m[Close]\033[0m closing the session ..."
session.close(3000, "Go away!")
end
end
run MyHelloWorld.new
end
EM.run do
thin = Rack::Handler.get("thin")
thin.run(app.to_app, Port: PORT)
end