MS socket now works in the server, but I can't figure out how to mock this stuff
This commit is contained in:
parent
a35892428c
commit
58ed75a289
9 changed files with 31 additions and 74 deletions
|
|
@ -5,6 +5,7 @@ class DashboardController < ApplicationController
|
|||
|
||||
def index
|
||||
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
|
||||
puts session.inspect
|
||||
end
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,13 +2,16 @@ class SocketController < ApplicationController
|
|||
include ApplicationHelper
|
||||
include SocketHelper
|
||||
include Rails.application.routes.url_helpers
|
||||
before_filter :authenticate_user!
|
||||
|
||||
def default_url_options()
|
||||
{:host=> 'example.com'}
|
||||
end
|
||||
|
||||
|
||||
# def default_url_options()
|
||||
# {:host=> 'example.com'}
|
||||
# end
|
||||
|
||||
def incoming(msg)
|
||||
puts msg
|
||||
puts "#{msg} connected!"
|
||||
end
|
||||
|
||||
def new_subscriber
|
||||
|
|
@ -16,32 +19,18 @@ class SocketController < ApplicationController
|
|||
end
|
||||
|
||||
def outgoing(object)
|
||||
puts "made it sucka"
|
||||
begin
|
||||
@_request = ActionDispatch::Request.new(:socket => true)
|
||||
WebSocket.push_to_clients(action_hash(object))
|
||||
rescue Exception => e
|
||||
puts e.inspect
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
def delete_subscriber(sid)
|
||||
WebSocket.unsubscribe(sid)
|
||||
end
|
||||
|
||||
|
||||
|
||||
# need a data strucutre to keep track of who is where
|
||||
|
||||
#the way this is set up now, we have users on pages
|
||||
|
||||
#could have... a channel for every page/collection...not that cool
|
||||
#or, have a single channel, which has a corresponding :current page => [sid]
|
||||
# can i cherry pick subscribers from a a channel?
|
||||
|
||||
|
||||
# we want all sorts of stuff that comes with being a controller
|
||||
# like, protect from forgery, view rendering, etc
|
||||
|
||||
|
||||
#these functions are not really routes
|
||||
#so the question is, whats the best way to call them?
|
||||
|
||||
#also, this is an input output controller
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ module ApplicationHelper
|
|||
end
|
||||
|
||||
def owner_picture
|
||||
default = "#{root_url}images/user/default.jpg"
|
||||
image = "#{root_url}images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg"
|
||||
default = "/images/user/default.jpg"
|
||||
image = "/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg"
|
||||
|
||||
if File.exist?("public/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg")
|
||||
image_tag image, :id => "user_picture"
|
||||
|
|
|
|||
|
|
@ -13,14 +13,15 @@ module SocketHelper
|
|||
begin
|
||||
v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction
|
||||
|
||||
|
||||
rescue Exception => e
|
||||
puts "in failzord " + v.inspect
|
||||
puts "web socket view rendering failed for some reason." + v.inspect
|
||||
puts object.inspect
|
||||
puts e.message
|
||||
raise e
|
||||
end
|
||||
|
||||
{:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}
|
||||
{:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}.to_json
|
||||
end
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -53,11 +53,11 @@ class Post
|
|||
end
|
||||
|
||||
def send_to_view
|
||||
WebSocket.push_to_clients(self)
|
||||
SocketController.new.outgoing(self)
|
||||
end
|
||||
|
||||
def remove_from_view
|
||||
WebSocket.push_to_clients(Retraction.for(self))
|
||||
SocketController.new.outgoing(Retraction.for(self))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
function debug(str){ $("#debug").append("<p>" + str); };
|
||||
|
||||
ws = new WebSocket("ws://#{request.host}:8080/");
|
||||
ws.onmessage = function(evt) {
|
||||
ws.onmessage = function(evt) {
|
||||
var obj = jQuery.parseJSON(evt.data);
|
||||
debug("got a " + obj['class']);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ module WebSocket
|
|||
:debug =>APP_CONFIG[:debug]) do |ws|
|
||||
ws.onopen {
|
||||
@ws = ws
|
||||
sid = SocketController.new.new_subscriber(ws)
|
||||
sid = SocketController.new.new_subscriber
|
||||
|
||||
ws.onmessage { |msg| SocketController.new.incoming(msg) }#@channel.push msg; puts msg}
|
||||
|
||||
|
|
@ -24,10 +24,10 @@ module WebSocket
|
|||
|
||||
def self.initialize_channel
|
||||
@channel = EM::Channel.new
|
||||
puts @channel.inspect
|
||||
end
|
||||
|
||||
def self.push_to_clients(html)
|
||||
puts html
|
||||
@channel.push(html)
|
||||
end
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ module WebSocket
|
|||
|
||||
|
||||
def self.subscribe
|
||||
@channel.subscribe{ |msg| puts "hello #{msg}";@ws.send msg }
|
||||
@channel.subscribe{ |msg| puts @ws.inspect; puts "ehllo" ; @ws.send msg }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
# module SocketRenderer
|
||||
# require 'app/helpers/application_helper'
|
||||
# def self.instantiate_view
|
||||
# @view = ActionView::Base.new(ActionController::Base.view_paths, {})
|
||||
# class << @view
|
||||
# include ApplicationHelper
|
||||
# include Rails.application.routes.url_helpers
|
||||
# include ActionController::RequestForgeryProtection::ClassMethods
|
||||
# def protect_against_forgery?
|
||||
# false
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def self.view_hash(object)
|
||||
# begin
|
||||
# v = view_for(object) unless object.is_a? Retraction
|
||||
#
|
||||
# rescue Exception => e
|
||||
# puts "in failzord " + v.inspect
|
||||
# puts object.inspect
|
||||
# puts e.message
|
||||
# raise e
|
||||
# end
|
||||
#
|
||||
# {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}
|
||||
# end
|
||||
#
|
||||
# def self.view_for(object)
|
||||
# @view.render @view.type_partial(object), :post => object
|
||||
# end
|
||||
#
|
||||
# def self.obj_id(object)
|
||||
# if object.is_a? Post
|
||||
# object.id
|
||||
# else
|
||||
# object.post_id
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
|
|
@ -24,6 +24,12 @@ RSpec.configure do |config|
|
|||
|
||||
config.before(:each) do
|
||||
DatabaseCleaner.start
|
||||
SocketController.stub!(:incoming).and_return(true)
|
||||
SocketController.stub!(:new_subscriber).and_return(true)
|
||||
SocketController.stub!(:outgoing).and_return(true)
|
||||
SocketController.stub!(:delete_subscriber).and_return(true)
|
||||
|
||||
|
||||
WebSocket.stub!(:push_to_clients).and_return("stub")
|
||||
WebSocket.stub!(:unsubscribe).and_return("stub")
|
||||
WebSocket.stub!(:subscribe).and_return("stub")
|
||||
|
|
|
|||
Loading…
Reference in a new issue