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
def index
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
end
end
end
......@@ -2,18 +2,14 @@ class SocketsController < ApplicationController
include ApplicationHelper
include SocketsHelper
include Rails.application.routes.url_helpers
before_filter :authenticate_user!
def incoming(msg)
puts "#{msg} connected!"
puts "Got a connection to: #{msg}"
end
def outgoing(object)
def outgoing(uid,object)
@_request = ActionDispatch::Request.new({})
WebSocket.push_to_clients(action_hash(object))
WebSocket.push_to_user(uid, action_hash(uid, object))
end
def delete_subscriber(sid)
WebSocket.unsubscribe(sid)
end
end
......@@ -9,9 +9,10 @@ module SocketsHelper
{:host => ""}
end
def action_hash(object)
def action_hash(uid, object)
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
Rails.logger.error("web socket view rendering failed for object #{object.inspect}.")
raise e
......
......@@ -76,7 +76,10 @@ protected
end
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
def remove_from_view
......
......@@ -7,7 +7,9 @@
$(document).ready(function(){
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) {
var obj = jQuery.parseJSON(evt.data);
debug("got a " + obj['class']);
......
development:
debug: false
debug: true
socket_port: 8080
pubsub_server: 'https://pubsubhubbub.appspot.com/'
......
......@@ -3,33 +3,40 @@ require 'eventmachine'
module WebSocket
EM.next_tick {
initialize_channel
initialize_channels
EventMachine::WebSocket.start(
:host => "0.0.0.0",
:port => APP_CONFIG[:socket_port],
:debug =>APP_CONFIG[:debug]) do |ws|
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.onclose { SocketsController.new.delete_subscriber(sid) }
ws.onclose { unsubscribe(ws.request['Path'].gsub('/',''), sid) }
}
end
}
def self.initialize_channel
@channel = EM::Channel.new
def self.initialize_channels
@channels = {}
end
def self.push_to_clients(html)
@channel.push(html)
def self.push_to_user(uid, data)
puts "Pushing to #{uid}"
@channels[uid.to_s].push(data) if @channels[uid.to_s]
end
def self.unsubscribe(sid)
@channel.unsubscribe(sid)
def self.subscribe(uid, ws)
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
......
=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