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.
75 76 77 |
# File 'lib/jschat/http/jschat.rb', line 75 def initialize( = nil) @cookie = end |
Instance Attribute Details
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
73 74 75 |
# File 'lib/jschat/http/jschat.rb', line 73 def @cookie end |
#identification_error ⇒ Object (readonly)
Returns the value of attribute identification_error.
73 74 75 |
# File 'lib/jschat/http/jschat.rb', line 73 def identification_error @identification_error end |
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
73 74 75 |
# File 'lib/jschat/http/jschat.rb', line 73 def last_error @last_error end |
Instance Method Details
#active? ⇒ Boolean
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/jschat/http/jschat.rb', line 132 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
147 148 149 |
# File 'lib/jschat/http/jschat.rb', line 147 def change(change_type, data) send_json({ 'change' => change_type, change_type => data }) end |
#connect ⇒ Object
83 84 85 86 |
# File 'lib/jschat/http/jschat.rb', line 83 def connect response = send_json({ :protocol => 'stateless' }) @cookie = response['cookie'] end |
#cookie_set? ⇒ Boolean
79 80 81 |
# File 'lib/jschat/http/jschat.rb', line 79 def !(@cookie.nil? or @cookie.empty?) end |
#identify(name, ip, session_length = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/jschat/http/jschat.rb', line 88 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
120 121 122 |
# File 'lib/jschat/http/jschat.rb', line 120 def join(room) send_json({ :join => room }, false) end |
#lastlog(room) ⇒ Object
102 103 104 105 |
# File 'lib/jschat/http/jschat.rb', line 102 def lastlog(room) response = send_json({ :lastlog => room }) response['messages'] end |
#names(room) ⇒ Object
151 152 153 |
# File 'lib/jschat/http/jschat.rb', line 151 def names(room) send_json({'names' => room}) end |
#part(room) ⇒ Object
124 125 126 |
# File 'lib/jschat/http/jschat.rb', line 124 def part(room) send_json({ :part => room }) end |
#ping ⇒ Object
143 144 145 |
# File 'lib/jschat/http/jschat.rb', line 143 def ping send_json({ 'ping' => Time.now.utc }) end |
#recent_messages(room) ⇒ Object
112 113 114 |
# File 'lib/jschat/http/jschat.rb', line 112 def (room) send_json({ 'since' => room })['messages'] end |
#room_update_times ⇒ Object
116 117 118 |
# File 'lib/jschat/http/jschat.rb', line 116 def room_update_times send_json({ 'times' => 'all' }) end |
#rooms ⇒ Object
98 99 100 |
# File 'lib/jschat/http/jschat.rb', line 98 def rooms send_json({ :list => 'rooms' }) end |
#search(phrase, room) ⇒ Object
107 108 109 110 |
# File 'lib/jschat/http/jschat.rb', line 107 def search(phrase, room) response = send_json({ :search => phrase, :room => room }) response['messages'] end |
#send_json(h, get_results = true) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/jschat/http/jschat.rb', line 159 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
128 129 130 |
# File 'lib/jschat/http/jschat.rb', line 128 def (, to) send_json({ :send => , :to => to }, false) end |
#send_quit(name) ⇒ Object
155 156 157 |
# File 'lib/jschat/http/jschat.rb', line 155 def send_quit(name) send_json({'quit' => name }) end |