我对 Ruby 和 Sinatra 很陌生,所以我不知道这个问题是客户端还是服务器端。尝试对应用程序进行 POST 时,我在控制台日志中收到 404 not found 错误。'/' 、 '/admin/ 和 '/connect' 页面工作得很好,只有 '/push' 没有找到。
Javascript:
$.post( '/arduino/public/push', notification,'json');
配置.ru
# encoding: UTF-8
require './stream'
run Sinatra::Application
流.rb
require 'json'
require 'sinatra'
set :public_folder, Proc.new{File.join(root,"public")}
set server: 'thin'
get '/' do
erb :index
end
get '/admin' do
erb :admin
end
def timestamp
Time.now.strftime("%H:%M:%S")
end
connections = []
notifications = []
get '/connect', provides: 'text/event-stream' do
stream :keep_open do |out|
connections << out
#out.callback on stream close evt.
out.callback{
#delete the connection
connections.delete(out)
}
end
end
post '/push' do
puts params
#Add the timestamp to the notification
notification = params.merge({'timestamp'=>timestamp}).to_json
notifications.shift if notifications.length > 10
connections.each{ |out| out << "data: #{notification}\n\n"}
end