Skip to content

Commit

Permalink
Finish moving out websocket server, add task to start it
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael committed Sep 17, 2010
1 parent b45906f commit 7c02200
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
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
Expand Down
40 changes: 40 additions & 0 deletions script/websocket_server.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions spec/lib/websocket_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7c02200

Please sign in to comment.