Class: JsChat::Bridge
- Inherits:
-
Object
- Object
- JsChat::Bridge
- Defined in:
- lib/jschat/http/jschat.rb
Overview
todo: can this be async and allow the server to have multiple threads?
Instance Attribute Summary collapse
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#identification_error ⇒ Object
readonly
Returns the value of attribute identification_error.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #change(change_type, data) ⇒ Object
- #connect ⇒ Object
- #cookie_set? ⇒ Boolean
- #identify(name, ip, session_length = nil) ⇒ Object
-
#initialize(cookie = nil) ⇒ Bridge
constructor
A new instance of Bridge.
- #join(room) ⇒ Object
- #lastlog(room) ⇒ Object
- #names(room) ⇒ Object
- #part(room) ⇒ Object
- #ping ⇒ Object
- #recent_messages(room) ⇒ Object
- #room_update_times ⇒ Object
- #rooms ⇒ Object
- #search(phrase, room) ⇒ Object
- #send_json(h, get_results = true) ⇒ Object
- #send_message(message, to) ⇒ Object
- #send_quit(name) ⇒ Object
Constructor Details
#initialize(cookie = nil) ⇒ Bridge
Returns a new instance of Bridge.
74 75 76 |
# File 'lib/jschat/http/jschat.rb', line 74 def initialize( = nil) @cookie = end |
Instance Attribute Details
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
72 73 74 |
# File 'lib/jschat/http/jschat.rb', line 72 def @cookie end |
#identification_error ⇒ Object (readonly)
Returns the value of attribute identification_error.
72 73 74 |
# File 'lib/jschat/http/jschat.rb', line 72 def identification_error @identification_error end |
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
72 73 74 |
# File 'lib/jschat/http/jschat.rb', line 72 def last_error @last_error end |
Instance Method Details
#active? ⇒ Boolean
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/jschat/http/jschat.rb', line 131 def active? return false unless response = ping if response.nil? or response['display'] == 'error' @last_error = response false else true end end |
#change(change_type, data) ⇒ Object
146 147 148 |
# File 'lib/jschat/http/jschat.rb', line 146 def change(change_type, data) send_json({ 'change' => change_type, change_type => data }) end |
#connect ⇒ Object
82 83 84 85 |
# File 'lib/jschat/http/jschat.rb', line 82 def connect response = send_json({ :protocol => 'stateless' }) @cookie = response['cookie'] end |
#cookie_set? ⇒ Boolean
78 79 80 |
# File 'lib/jschat/http/jschat.rb', line 78 def !(@cookie.nil? or @cookie.empty?) end |
#identify(name, ip, session_length = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/jschat/http/jschat.rb', line 87 def identify(name, ip, session_length = nil) response = send_json({ :identify => name, :ip => ip, :session_length => session_length }) if response['display'] == 'error' @identification_error = response false else true end end |
#join(room) ⇒ Object
119 120 121 |
# File 'lib/jschat/http/jschat.rb', line 119 def join(room) send_json({ :join => room }, false) end |
#lastlog(room) ⇒ Object
101 102 103 104 |
# File 'lib/jschat/http/jschat.rb', line 101 def lastlog(room) response = send_json({ :lastlog => room }) response['messages'] end |
#names(room) ⇒ Object
150 151 152 |
# File 'lib/jschat/http/jschat.rb', line 150 def names(room) send_json({'names' => room}) end |
#part(room) ⇒ Object
123 124 125 |
# File 'lib/jschat/http/jschat.rb', line 123 def part(room) send_json({ :part => room }) end |
#ping ⇒ Object
142 143 144 |
# File 'lib/jschat/http/jschat.rb', line 142 def ping send_json({ 'ping' => Time.now.utc }) end |
#recent_messages(room) ⇒ Object
111 112 113 |
# File 'lib/jschat/http/jschat.rb', line 111 def (room) send_json({ 'since' => room })['messages'] end |
#room_update_times ⇒ Object
115 116 117 |
# File 'lib/jschat/http/jschat.rb', line 115 def room_update_times send_json({ 'times' => 'all' }) end |
#rooms ⇒ Object
97 98 99 |
# File 'lib/jschat/http/jschat.rb', line 97 def rooms send_json({ :list => 'rooms' }) end |
#search(phrase, room) ⇒ Object
106 107 108 109 |
# File 'lib/jschat/http/jschat.rb', line 106 def search(phrase, room) response = send_json({ :search => phrase, :room => room }) response['messages'] end |
#send_json(h, get_results = true) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jschat/http/jschat.rb', line 158 def send_json(h, get_results = true) response = nil h[:cookie] = @cookie if c = TCPSocket.open(ServerConfig['ip'], ServerConfig['port']) c.send(h.to_json + "\n", 0) if get_results response = c.gets response = JSON.parse(response) end ensure c.close response end |
#send_message(message, to) ⇒ Object
127 128 129 |
# File 'lib/jschat/http/jschat.rb', line 127 def (, to) send_json({ :send => , :to => to }, false) end |
#send_quit(name) ⇒ Object
154 155 156 |
# File 'lib/jschat/http/jschat.rb', line 154 def send_quit(name) send_json({'quit' => name }) end |