Skip to content
Extraits de code Groupes Projets
Valider 33a827c3 rédigé par Raphael's avatar Raphael
Parcourir les fichiers

RS, DG; Websocket is now working again, users have a channel

parent 0a40efda
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,5 +5,4 @@ class DashboardsController < ApplicationController ...@@ -5,5 +5,4 @@ class DashboardsController < ApplicationController
def index def index
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC' @posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
end end
end
end
...@@ -2,18 +2,14 @@ class SocketsController < ApplicationController ...@@ -2,18 +2,14 @@ class SocketsController < ApplicationController
include ApplicationHelper include ApplicationHelper
include SocketsHelper include SocketsHelper
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
before_filter :authenticate_user!
def incoming(msg) def incoming(msg)
puts "#{msg} connected!" puts "Got a connection to: #{msg}"
end end
def outgoing(object) def outgoing(uid,object)
@_request = ActionDispatch::Request.new({}) @_request = ActionDispatch::Request.new({})
WebSocket.push_to_clients(action_hash(object)) WebSocket.push_to_user(uid, action_hash(uid, object))
end end
def delete_subscriber(sid)
WebSocket.unsubscribe(sid)
end
end end
...@@ -9,9 +9,10 @@ module SocketsHelper ...@@ -9,9 +9,10 @@ module SocketsHelper
{:host => ""} {:host => ""}
end end
def action_hash(object) def action_hash(uid, object)
begin begin
v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction user = User.first(:id => uid)
v = render_to_string(:partial => type_partial(object), :locals => {:post => object, :current_user => user}) unless object.is_a? Retraction
rescue Exception => e rescue Exception => e
Rails.logger.error("web socket view rendering failed for object #{object.inspect}.") Rails.logger.error("web socket view rendering failed for object #{object.inspect}.")
raise e raise e
......
...@@ -76,7 +76,10 @@ protected ...@@ -76,7 +76,10 @@ protected
end end
def send_to_view def send_to_view
SocketsController.new.outgoing(self) people_with_permissions.each{|f|
SocketsController.new.outgoing(f.owner_id, self) if f.owner_id
}
SocketsController.new.outgoing(person.owner_id, self) if person.owner_id
end end
def remove_from_view def remove_from_view
......
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
$(document).ready(function(){ $(document).ready(function(){
function debug(str){ $("#debug").append("<p>" + str); }; function debug(str){ $("#debug").append("<p>" + str); };
ws = new WebSocket("ws://#{request.host}:8080/"); ws = new WebSocket("ws://#{request.host}:8080/#{CGI::escape(current_user.id.to_s)}");
//Attach onmessage to websocket
ws.onmessage = function(evt) { ws.onmessage = function(evt) {
var obj = jQuery.parseJSON(evt.data); var obj = jQuery.parseJSON(evt.data);
debug("got a " + obj['class']); debug("got a " + obj['class']);
......
development: development:
debug: false debug: true
socket_port: 8080 socket_port: 8080
pubsub_server: 'https://pubsubhubbub.appspot.com/' pubsub_server: 'https://pubsubhubbub.appspot.com/'
......
...@@ -3,33 +3,40 @@ require 'eventmachine' ...@@ -3,33 +3,40 @@ require 'eventmachine'
module WebSocket module WebSocket
EM.next_tick { EM.next_tick {
initialize_channel initialize_channels
EventMachine::WebSocket.start( EventMachine::WebSocket.start(
:host => "0.0.0.0", :host => "0.0.0.0",
:port => APP_CONFIG[:socket_port], :port => APP_CONFIG[:socket_port],
:debug =>APP_CONFIG[:debug]) do |ws| :debug =>APP_CONFIG[:debug]) do |ws|
ws.onopen { ws.onopen {
@ws = ws
sid = @channel.subscribe{ |msg| ws.send msg }#SocketsController.new.new_subscriber sid = self.subscribe(ws.request['Path'].gsub('/',''), ws)
ws.onmessage { |msg| SocketsController.new.incoming(msg) }#@channel.push msg; puts msg} ws.onmessage { |msg| SocketsController.new.incoming(msg) }#@channel.push msg; puts msg}
ws.onclose { SocketsController.new.delete_subscriber(sid) } ws.onclose { unsubscribe(ws.request['Path'].gsub('/',''), sid) }
} }
end end
} }
def self.initialize_channel def self.initialize_channels
@channel = EM::Channel.new @channels = {}
end end
def self.push_to_clients(html) def self.push_to_user(uid, data)
@channel.push(html) puts "Pushing to #{uid}"
@channels[uid.to_s].push(data) if @channels[uid.to_s]
end end
def self.unsubscribe(sid) def self.subscribe(uid, ws)
@channel.unsubscribe(sid) puts "Subscribing #{uid}"
@channels[uid] ||= EM::Channel.new
@channels[uid].subscribe{ |msg| ws.send msg }
end
def self.unsubscribe(uid,sid)
@channels[uid].unsubscribe(sid) if @channels[uid]
end end
end end
......
=begin
require File.dirname(__FILE__) + '/../spec_helper'
describe SocketRenderer do
before do
SocketRenderer.instantiate_view
@user = Factory.create(:user, :email => "bob@jones.com")
@user.profile = Factory.create(:profile, :person => @user)
end
it 'should render a partial for a status message' do
message = Factory.create(:status_message, :person => @user)
html = SocketRenderer.view_for message
html.include? message.message
end
it 'should prepare a class/view hash' do
message = Factory.create(:status_message, :person => @user)
hash = SocketRenderer.view_hash(message)
hash[:class].should == "status_messages"
end
end
=end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter