Ruby 1.9.3およびThin 1.3.1を使用して、同じポートを使用して、通常のHTTPトラフィックだけでなく、Cramp(EventMachineの上に構築されています)経由でHTML5 Webソケットを提供しようとしています。ここでは、最小限の、自己完結型の例である:同じポート上にCramp :: Websocketと通常のRackアプリケーションを提供するには?
require 'thin'
require 'cramp'
require 'http_router'
Cramp::Websocket.backend = :thin
class SocketApp < Cramp::Action
self.transport = :websocket
on_start = :connected
on_finish = :disconnected
on_data = :message
def connected
puts 'Client connected'
end
def disconnected
puts 'Client disconnected'
end
def message(msg)
puts "Got message: #{msg}"
render 'Here is your reply'
end
end
class WebApp
def call(env)
[ 200, { 'Content-Type' => 'text/html' }, <<EOF
<html><head>
<script>
function init() {
function log(msg) { document.getElementById('log').innerHTML += msg + '<br>'; }
var socketUri = 'ws://' + document.location.host + '/socket';
log('Socket URI: ' + socketUri);
var socket = new WebSocket(socketUri);
socket.onopen = function(e) {
log('onopen');
socket.send('Is there anybody out there?');
log('sent message');
};
socket.onclose = function(e) {
log('onclose; code = ' + e.code + ', reason = ' + e.reason);
};
socket.onerror = function(e) {
log('onerror');
};
socket.onmessage = function(e) {
log('onmessage; data = ' + e.data);
};
}
</script>
</head><body onload='init();'>
<h1>Serving Cramp::Websocket and normal Rack app on the same port</h1>
<p id='log'></p>
</body></html>
EOF
]
end
end
app = HttpRouter.new do
add('/socket').to SocketApp
add('/').to WebApp.new
end
run app
あなたは、自分のためにこれを試してみてくださいconfig.ru
という名前のファイルにこのコードを貼り付けしてthin start
を実行したい場合。 thin
、cramp
、http_router
がインストールされている必要があります。
考えられるのは、JavaScriptコードがWebSocket接続をws://localhost:3000/socket
にして送信されたメッセージをエコーしますが、意図したとおりに動作しないという考えです。 open
イベントが発生すると、メッセージの送信時にエラーは発生しませんが、応答は得られません。
Client connected
メッセージが印刷されないため、サーバーの観点からは接続されていません。
thin start -D
を使用すると、HTTP 101が発生し、一部のバイナリデータが交換されていることがわかります。
私は間違っていますか?
更新:私は2つの部分にファイルを分割した場合、HttpRouter
をリッピングし、異なるポート上の2つのthin
のインスタンスを実行し、それはまだ動作しません。問題はソケットコードにあり、HttpRouter
またはWebApp
ではなくです。