我正在尝试使用sinatra/websocketio
gem,我基本上是从他们的README中复制粘贴的,我无法让它工作。我错过了什么吗?我认为socket端口和Sinatra端口应该是一样的。我感谢任何帮助或提示。
JavaScript的:
<script>
var io = new WebSocketIO().connect();
// io.on("log", function(value) {
// alert(value)
// });
</script>
Sinatra Server:
require 'sinatra'
require 'sinatra/websocketio'
set :port, 5005
set :bind, '0.0.0.0'
set :public_folder, "#{File.dirname(__FILE__)}/views"
set :websocketio, port: 9000
enable :sessions
get '/list' do
"Hello world!"
end
get '/' do
erb :index, layout: :layout
end
JavaScript端无法连接:
答案 0 :(得分:1)
看起来像Sinatra::WebSocketIO
only starts up once an EventMachine reactor is running。运行基于EventMachine的Rack服务器(例如Thin)时就是这种情况。将Rack服务器切换为Thin可以很好地启动WebSocketIO服务器。
如果不能选择切换到Thin,您也可以在config.ru
内启动EventMachine反应器:
require_relative "./server" # assuming your server code is in `./server.rb`
run Sinatra::Application
# EventMachine.run blocks the active thread, therefore it's started in a separate one
Thread.new { EventMachine.run }
然后可以使用bundle exec rackup -p 5005