Class: WebIM::Client
- Inherits:
-
Object
- Object
- WebIM::Client
- Defined in:
- lib/webim.rb
Overview
A Ruby webim client library for NexTalk.im.
Constant Summary collapse
- DEFAULT_PORT =
Default port.
8000
Instance Attribute Summary collapse
-
#apikey ⇒ Object
readonly
通信APIKEY.
-
#domain ⇒ Object
readonly
网站域名.
-
#endpoint ⇒ Object
readonly
用户Hash对象.
-
#host ⇒ Object
readonly
消息服务器地址.
-
#port ⇒ Object
readonly
消息服务器端口.
-
#ticket ⇒ Object
readonly
消息服务器通信令牌.
-
#timeout ⇒ Object
readonly
HTTP Timeout.
Instance Method Summary collapse
-
#initialize(endpoint, domain, apikey, host = 'localhost', port = 8000, ticket = nil, timeout = 15) ⇒ Client
constructor
创建Client对象.
-
#inspect ⇒ Object
Clien对象描述.
-
#join(room) ⇒ Object
转发加入群组消息.
-
#leave(room) ⇒ Object
转发离开群组消息.
-
#members(room) ⇒ Object
从消息服务器读取当前群组在线用户.
-
#message(message) ⇒ Object
type: chat | grpchat style: 消息CSS.
-
#offline ⇒ Object
向消息服务器转发用户下线消息.
-
#online(buddies, rooms) ⇒ Object
向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表.
-
#presence(show, status = "") ⇒ Object
向消息服务器转发用户现场变化.
-
#presences(ids) ⇒ Object
从消息服务器读取当前在线用户.
-
#status(to, show, status = "") ⇒ Object
向消息服务器转发用户状态,例如正在输入…
Constructor Details
#initialize(endpoint, domain, apikey, host = 'localhost', port = 8000, ticket = nil, timeout = 15) ⇒ Client
创建Client对象
endpoint: 用户Hash对象domain: 域名apikey: 通信APIKEY host: 服务器地址port: 服务器端口号ticket: 通信令牌timeout: HTTP超时时间
79 80 81 82 83 84 85 86 87 |
# File 'lib/webim.rb', line 79 def initialize(endpoint, domain, apikey, host = 'localhost', port = 8000, ticket = nil, timeout = 15) @endpoint = endpoint @domain = domain @apikey = apikey @host = host @port = port @ticket = ticket @timeout = timeout end |
Instance Attribute Details
#apikey ⇒ Object (readonly)
通信APIKEY
47 48 49 |
# File 'lib/webim.rb', line 47 def apikey @apikey end |
#domain ⇒ Object (readonly)
网站域名
42 43 44 |
# File 'lib/webim.rb', line 42 def domain @domain end |
#endpoint ⇒ Object (readonly)
用户Hash对象
属性:
id: 用户id
nick: 用户昵称
show: 'available' | 'away' | 'chat' | 'dnd' | 'invisible' | 'unavailable'
status: 状态信息
37 38 39 |
# File 'lib/webim.rb', line 37 def endpoint @endpoint end |
#host ⇒ Object (readonly)
消息服务器地址
52 53 54 |
# File 'lib/webim.rb', line 52 def host @host end |
#port ⇒ Object (readonly)
消息服务器端口
57 58 59 |
# File 'lib/webim.rb', line 57 def port @port end |
#ticket ⇒ Object (readonly)
消息服务器通信令牌
62 63 64 |
# File 'lib/webim.rb', line 62 def ticket @ticket end |
#timeout ⇒ Object (readonly)
HTTP Timeout
66 67 68 |
# File 'lib/webim.rb', line 66 def timeout @timeout end |
Instance Method Details
#inspect ⇒ Object
Clien对象描述
226 227 228 229 |
# File 'lib/webim.rb', line 226 def inspect "<Webim::Client: ticket=%s, domain=%s, apikey=%s, host=%s, port=%p>" % [@ticket, @domain, @apikey, @host, @port] end |
#join(room) ⇒ Object
转发加入群组消息
room: 群组id
205 206 207 208 209 210 |
# File 'lib/webim.rb', line 205 def join(room) httpost("/rooms/#{room}/join", reqdata().merge({ :nick => @endpoint[:nick], :room => room })) end |
#leave(room) ⇒ Object
转发离开群组消息
room: 群组id
217 218 219 220 221 222 |
# File 'lib/webim.rb', line 217 def leave(room) httpost("/rooms/#{room}/leave", reqdata().merge({ :nick => @endpoint[:nick], :room => room })) end |
#members(room) ⇒ Object
从消息服务器读取当前群组在线用户
room: 群组room
194 195 196 197 198 |
# File 'lib/webim.rb', line 194 def members(room) httpget("/rooms/#{room}/members", reqdata().merge({ :room => room })) end |
#message(message) ⇒ Object
type: chat | grpchat
style: 消息CSS
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/webim.rb', line 150 def () reqdata = reqdata() reqdata.merge!({ :nick => @endpoint[:nick], :type => "chat", :style => "", :timestamp => Time.now.to_f * 1000 }) reqdata.merge!() httpost('/messages', reqdata) end |
#offline ⇒ Object
向消息服务器转发用户下线消息
123 124 125 |
# File 'lib/webim.rb', line 123 def offline httpost('/presences/offline', reqdata()) end |
#online(buddies, rooms) ⇒ Object
向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表
buddies: 好友id列表
rooms: 群组id列表
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/webim.rb', line 95 def online(buddies, rooms) resp = httpost('/presences/online', reqdata().merge({ :buddies => buddies.join(','), :rooms => rooms.join(','), :name => @endpoint[:id], :nick => @endpoint[:nick], :show => @endpoint[:show], :status => @endpoint[:status] })) @ticket = resp["ticket"] connection = { :ticket => @ticket, :domain => @domain, :server => resp['server'], :jsonpd => resp['jsonpd'] } connection[:websocket] = resp['websocket'] if resp['websocket'] connection[:mqtt] = resp['mqtt'] if resp['mqtt'] { :success => true, :connection => connection, :presences => resp['presences'] } end |
#presence(show, status = "") ⇒ Object
向消息服务器转发用户现场变化
show: 'away' | 'chat' | 'dnd' | 'invisible'
status: 状态信息
133 134 135 136 137 138 139 |
# File 'lib/webim.rb', line 133 def presence(show, status = "") httpost('/presences/show', reqdata().merge({ :nick => @endpoint[:nick], :show => show, :status => status })) end |
#presences(ids) ⇒ Object
从消息服务器读取当前在线用户
ids: 用户id列表
183 184 185 186 187 |
# File 'lib/webim.rb', line 183 def presences(ids) httpget('/presences', reqdata().merge({ :ids => ids.join(",") })) end |
#status(to, show, status = "") ⇒ Object
向消息服务器转发用户状态,例如正在输入…
to: 接受者
show: typing
status: 状态信息
169 170 171 172 173 174 175 176 |
# File 'lib/webim.rb', line 169 def status(to, show, status="") httpost('/statuses', reqdata().merge({ :nick => @endpoint[:nick], :to => to, :show => show, :status => status })) end |