Skip to content

Commit

Permalink
Make websocket host and port configurable. Also
Browse files Browse the repository at this point in the history
make webserver host and port configurable.
  • Loading branch information
pusewicz committed May 11, 2010
1 parent d20be41 commit e3539e9
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 62 deletions.
115 changes: 62 additions & 53 deletions example/config.yml.example
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
Twitter:
name: twitter
title: Our Tweets
nitems: 5
username: username
password: password
follow: soniaappcom

Tfl:
name: tfl
title: TfL Status

Foursquare:
name: foursquare
title: Where we are
username: username_email_or_mobile_number
password: password

Campfire:
nitems: 5
name: campfire
title: Campfire
room_id: 1
url: "https://sample.campfirenow.com"
token: your_api_key

Icinga:
name: icinga
username: icingaadmin
password: icingaadmin
title: Icinga Status
url: "http://localhost/icinga/cgi-bin/tac.cgi"

Github:
title: Github Commits
name: github
username: your_username
token: your_token
nitems: 5

YahooWeather:
title: Weather for Paris
name: yahoo_weather
woeid: 615702
units: celsius

RSS:
name: RSS
title: Feeds
poll_time: 20
url: "http://www.engadget.com/rss.xml"
xpath: "//item//title"
nitems: 5
webserver:
host: localhost
port: 8080

websocket:
host: localhost
port: 9090

widgets:
Twitter:
name: twitter
title: Our Tweets
nitems: 5
username: username
password: password
follow: soniaappcom

Tfl:
name: tfl
title: TfL Status

Foursquare:
name: foursquare
title: Where we are
username: username_email_or_mobile_number
password: password

Campfire:
nitems: 5
name: campfire
title: Campfire
room_id: 1
url: "https://sample.campfirenow.com"
token: your_api_key

Icinga:
name: icinga
username: icingaadmin
password: icingaadmin
title: Icinga Status
url: "http://localhost/icinga/cgi-bin/tac.cgi"

Github:
title: Github Commits
name: github
username: your_username
token: your_token
nitems: 5

YahooWeather:
title: Weather for Paris
name: yahoo_weather
woeid: 615702
units: celsius

RSS:
name: RSS
title: Feeds
poll_time: 20
url: "http://www.engadget.com/rss.xml"
xpath: "//item//title"
nitems: 5
17 changes: 13 additions & 4 deletions lib/sonia/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@ def widget_stylesheets
end
end

def websocket_host
Sonia::Server.websocket_host
end

def websocket_port
Sonia::Server.websocket_port
end

def websocket_url
Sonia::Server.websocket_url
end

def joined_system_javascript
files = %w(
/vendor/swfobject.js
/vendor/console.js
/vendor/FABridge.js
/vendor/web_socket.js
/vendor/json2.js
Expand Down Expand Up @@ -60,10 +73,6 @@ def joined_widget_css
joined_javascript Dir[Sonia.root + "/widgets/*/*.css"]
end

def init_javascript
File.read(File.join(Sonia.root, "public", "javascripts", "init.js"))
end

private
def joined_javascript(files)
javascript = ""
Expand Down
48 changes: 44 additions & 4 deletions lib/sonia/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Sonia
module Server
extend self

# Defaults
WEBSOCKET_HOST = "localhost"
WEBSOCKET_PORT = 8080

Expand Down Expand Up @@ -77,7 +78,7 @@ def serve
def initialize_widgets
@widgets = []

config.each do |widget, config|
config.widgets.each do |widget, config|
class_name = "Sonia::Widgets::#{widget.to_s}"
log.info("Server") { "Created widget #{widget} with #{config.inspect}" }
@widgets << module_eval(class_name).new(config)
Expand All @@ -88,7 +89,21 @@ def initialize_widgets
#
# @return [Hash] Websocket's host and port number
def websocket_options
{ :host => WEBSOCKET_HOST, :port => WEBSOCKET_PORT }
{ :host => websocket_host, :port => websocket_port }
end

# Returns configured websocket hostname
#
# @return [String] Websocket hostname
def websocket_host
config.websocket.host || WEBSOCKET_HOST
end

# Returns configured websocket port
#
# @return [String] Websocket port
def websocket_port
config.websocket.port || WEBSOCKET_PORT
end

# Starts WebSocket server
Expand Down Expand Up @@ -126,14 +141,39 @@ def start_web_socket_server

# Starts Thin WebServer
def start_web_server
Thin::Server.start(WEBSERVER_HOST, WEBSERVER_PORT, ::Sonia::WebServer)
Thin::Server.start(
webserver_host,
webserver_port,
::Sonia::WebServer
)
end

# Returns configured webserver host
#
# @return [String] Webserver host
def webserver_host
config.webserver.host || WEBSERVER_HOST
end

# Returns configured webserver port
#
# @return [String] webserver port
def webserver_port
config.webserver.port || WEBSERVER_PORT
end

# Returns WebServer URL
#
# @return [String] WebServer URL
def webserver_url
"http://#{WEBSERVER_HOST}:#{WEBSERVER_PORT}"
"http://#{webserver_host}:#{webserver_port}"
end

# Returns WebSocket URL
#
# @return [String] WebSocket URL
def websocket_url
"ws://#{websocket_host}:#{websocket_port}"
end
end
end
16 changes: 15 additions & 1 deletion views/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@
%style(type="text/css")= joined_widget_css
%script(type="text/javascript")= joined_system_javascript
%script(type="text/javascript")= joined_widget_javascript
%script(type="text/javascript")= init_javascript
:javascript
WebSocket.__swfLocation = "WebSocketMain.swf";

$(document).observe("dom:loaded", function() {
if ("WebSocket" in window) {
console.log("WebSocket supported.");
window.sonia = new Sonia("#{websocket_url}");

Event.observe(window, 'beforeunload', function() {
window.sonia.saveChanges();
});
} else {
console.log("WebSocket not supported.");
}
});

0 comments on commit e3539e9

Please sign in to comment.