Finish moving out websocket server, add task to start it
This commit is contained in:
parent
b45906f902
commit
7c02200a67
3 changed files with 69 additions and 0 deletions
|
|
@ -58,6 +58,11 @@ namespace :deploy do
|
|||
task :start do
|
||||
start_mongo
|
||||
start_thin
|
||||
start_websocket
|
||||
end
|
||||
|
||||
task :start_websocket do
|
||||
run("cd #{current_path} && bundle exec ruby ./script/websocket_server.rb > /dev/null&")
|
||||
end
|
||||
|
||||
task :start_mongo do
|
||||
|
|
|
|||
40
script/websocket_server.rb
Normal file
40
script/websocket_server.rb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
require File.dirname(__FILE__) + '/../config/environment'
|
||||
require File.dirname(__FILE__) + '/../lib/diaspora/websocket'
|
||||
|
||||
CHANNEL = Magent::GenericChannel.new('websocket')
|
||||
def process_message
|
||||
if CHANNEL.queue_count > 0
|
||||
message = CHANNEL.dequeue
|
||||
if message
|
||||
Diaspora::WebSocket.push_to_user(message['uid'], message['data'])
|
||||
end
|
||||
EM.next_tick{ process_message}
|
||||
else
|
||||
EM::Timer.new(1){process_message}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
EM.run {
|
||||
Diaspora::WebSocket.initialize_channels
|
||||
|
||||
EventMachine::WebSocket.start(
|
||||
:host => "0.0.0.0",
|
||||
:port => APP_CONFIG[:socket_port],
|
||||
:debug =>APP_CONFIG[:socket_debug]) do |ws|
|
||||
ws.onopen {
|
||||
|
||||
sid = Diaspora::WebSocket.subscribe(ws.request['Path'].gsub('/',''), ws)
|
||||
|
||||
ws.onmessage { |msg| SocketsController.new.incoming(msg) }
|
||||
|
||||
ws.onclose { Diaspora::WebSocket.unsubscribe(ws.request['Path'].gsub('/',''), sid) }
|
||||
}
|
||||
end
|
||||
process_message
|
||||
}
|
||||
|
||||
24
spec/lib/websocket_spec.rb
Normal file
24
spec/lib/websocket_spec.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
require File.dirname(__FILE__) + '/../spec_helper'
|
||||
|
||||
describe Diaspora::WebSocket do
|
||||
before do
|
||||
@user = Factory.create(:user)
|
||||
@aspect = @user.aspect(:name => "losers")
|
||||
@post = @user.build_post(:status_message, :message => "hey", :to => @aspect.id)
|
||||
end
|
||||
|
||||
it 'should queue a job' do
|
||||
Diaspora::WebSocket.should_receive(:queue_to_user)
|
||||
@post.socket_to_uid(@user.id, :aspect_ids => @aspect.id)
|
||||
end
|
||||
|
||||
it 'The queued job should reach Magent' do
|
||||
Magent.should_receive(:push)
|
||||
@post.socket_to_uid(@user.id, :aspect_ids => @aspect.id)
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Reference in a new issue