prototyping server sent events
This commit is contained in:
parent
d7f4dc5fd2
commit
e17b6aa162
3 changed files with 64 additions and 1 deletions
44
app/controllers/api/v1/activity_log_controller.rb
Normal file
44
app/controllers/api/v1/activity_log_controller.rb
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Api::V1::ActivityLogController < Api::BaseController
|
||||||
|
include ActionController::Live
|
||||||
|
|
||||||
|
# before_action -> { doorkeeper_authorize! :read, :'read:lists' }, only: [:index, :show]
|
||||||
|
# before_action -> { doorkeeper_authorize! :write, :'write:lists' }, except: [:index, :show]
|
||||||
|
|
||||||
|
# before_action :require_user!
|
||||||
|
# before_action :set_list, except: [:index, :create]
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
class Foo
|
||||||
|
attr_accessor :test
|
||||||
|
end
|
||||||
|
|
||||||
|
rescue_from ArgumentError do |e|
|
||||||
|
render json: { error: e.to_s }, status: 422
|
||||||
|
end
|
||||||
|
|
||||||
|
def index
|
||||||
|
render :text => "hello world"
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
response.headers['Content-Type'] = 'text/event-stream'
|
||||||
|
# hack to avoid computing Etag, which delays sending of data
|
||||||
|
response.headers['Last-Modified'] = Time.now.httpdate
|
||||||
|
|
||||||
|
sse = SSE.new(response.stream)
|
||||||
|
|
||||||
|
begin
|
||||||
|
100.times {
|
||||||
|
f = Foo.new
|
||||||
|
f.test = "xxx"
|
||||||
|
sse.write f.to_json
|
||||||
|
sleep 1
|
||||||
|
}
|
||||||
|
ensure
|
||||||
|
sse.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -30,8 +30,25 @@ class Lists extends ImmutablePureComponent {
|
||||||
multiColumn: PropTypes.bool,
|
multiColumn: PropTypes.bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
state = {
|
||||||
|
logs: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.eventSource = new EventSource('/api/v1/activity_log');
|
||||||
|
this.eventSource.onmessage = (event) => {
|
||||||
|
this.setState({ logs: [...this.state.logs, JSON.parse(event.data)] });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
if (this.eventSource) {
|
||||||
|
this.eventSource.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
return <div>Hello world</div>;
|
return <div>{this.state.logs.map(x => (<div>{JSON.stringify(x)}</div>))}</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -542,6 +542,8 @@ Rails.application.routes.draw do
|
||||||
resources :confirmations, only: [:create]
|
resources :confirmations, only: [:create]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resource :activity_log, only: [:show], controller: 'activity_log'
|
||||||
|
|
||||||
resource :instance, only: [:show] do
|
resource :instance, only: [:show] do
|
||||||
resources :peers, only: [:index], controller: 'instances/peers'
|
resources :peers, only: [:index], controller: 'instances/peers'
|
||||||
resources :rules, only: [:index], controller: 'instances/rules'
|
resources :rules, only: [:index], controller: 'instances/rules'
|
||||||
|
|
Loading…
Add table
Reference in a new issue