forked from diaspora/diaspora
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish moving out websocket server, add task to start it
- Loading branch information
Raphael
committed
Sep 17, 2010
1 parent
b45906f
commit 7c02200
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |