PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/project/network/jwsChannelPlugIn.js

https://gitlab.com/BGCX261/zlatnaspirala2-svn-to-git
JavaScript | 366 lines | 229 code | 21 blank | 116 comment | 63 complexity | 1fc6f24f5c96343443010490eca87c42 MD5 | raw file
  1. // ---------------------------------------------------------------------------
  2. // jWebSocket Channel PlugIn (uses jWebSocket Client and Server)
  3. // (C) 2010 jWebSocket.org, Alexander Schulze, Innotrade GmbH, Herzogenrath
  4. // ---------------------------------------------------------------------------
  5. // This program is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License as published by the
  7. // Free Software Foundation; either version 3 of the License, or (at your
  8. // option) any later version.
  9. // This program is distributed in the hope that it will be useful, but WITHOUT
  10. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  12. // more details.
  13. // You should have received a copy of the GNU Lesser General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
  15. // ---------------------------------------------------------------------------
  16. // ---------------------------------------------------------------------------
  17. // jWebSocket Channel Plug-In
  18. // ---------------------------------------------------------------------------
  19. //:package:*:jws
  20. //:class:*:jws.ChannelPlugIn
  21. //:ancestor:*:-
  22. //:d:en:Implementation of the [tt]jws.ChannelPlugIn[/tt] class. This _
  23. //:d:en:plug-in provides the methods to subscribe and unsubscribe at certain _
  24. //:d:en:channel sn the server.
  25. jws.ChannelPlugIn = {
  26. //:const:*:NS:String:org.jwebsocket.plugins.channels (jws.NS_BASE + ".plugins.channels")
  27. //:d:en:Namespace for the [tt]ChannelPlugIn[/tt] class.
  28. // if namespace changes update server plug-in accordingly!
  29. NS: jws.NS_BASE + ".plugins.channels",
  30. SUBSCRIBE: "subscribe",
  31. UNSUBSCRIBE: "unsubscribe",
  32. GET_CHANNELS: "getChannels",
  33. CREATE_CHANNEL: "createChannel",
  34. REMOVE_CHANNEL: "removeChannel",
  35. GET_SUBSCRIBERS: "getSubscribers",
  36. GET_SUBSCRIPTIONS: "getSubscriptions",
  37. AUTHORIZE: "authorize",
  38. PUBLISH: "publish",
  39. STOP: "stop",
  40. processToken: function( aToken ) {
  41. // check if namespace matches
  42. if( aToken.ns == jws.ChannelPlugIn.NS ) {
  43. // here you can handle incoming tokens from the server
  44. // directy in the plug-in if desired.
  45. if( "event" == aToken.type ) {
  46. if( "channelCreated" == aToken.name ) {
  47. if( this.fOnChannelCreated ) {
  48. this.fOnChannelCreated( aToken );
  49. }
  50. } else if( "channelRemoved" == aToken.name ) {
  51. if( this.fOnChannelRemoved ) {
  52. this.fOnChannelRemoved( aToken );
  53. }
  54. }
  55. } else if( "getChannels" == aToken.reqType ) {
  56. if( this.fOnChannelsReceived ) {
  57. this.fOnChannelsReceived( aToken );
  58. }
  59. }
  60. }
  61. },
  62. //:m:*:channelSubscribe
  63. //:d:en:Registers the client at the given channel on the server. _
  64. //:d:en:After this operation the client obtains all messages on this _
  65. //:d:en:channel. Basically, a client can subscribe at multiple channels.
  66. //:d:en:If no channel with the given ID exists on the server an error token _
  67. //:d:en:is returned. Depending on the type of the channel it may take more _
  68. //:d:en:or less time until you get the first token from the channel.
  69. //:a:en::aChannel:String:The id of the server side data channel.
  70. //:r:*:::void:none
  71. // TODO: introduce OnResponse here too to get notified on error or success.
  72. channelSubscribe: function( aChannel, aAccessKey, aOptions ) {
  73. var lRes = this.checkConnected();
  74. if( 0 == lRes.code ) {
  75. this.sendToken({
  76. ns: jws.ChannelPlugIn.NS,
  77. type: jws.ChannelPlugIn.SUBSCRIBE,
  78. channel: aChannel,
  79. accessKey: aAccessKey
  80. }, aOptions );
  81. }
  82. return lRes;
  83. },
  84. //:m:*:channelUnsubscribe
  85. //:d:en:Unsubscribes the client from the given channel on the server.
  86. //:d:en:From this point in time the client does not receive any messages _
  87. //:d:en:on this channel anymore.
  88. //:a:en::aChannel:String:The id of the server side data channel.
  89. //:r:*:::void:none
  90. // TODO: introduce OnResponse here too to get notified on error or success.
  91. channelUnsubscribe: function( aChannel, aOptions ) {
  92. var lRes = this.checkConnected();
  93. if( 0 == lRes.code ) {
  94. this.sendToken({
  95. ns: jws.ChannelPlugIn.NS,
  96. type: jws.ChannelPlugIn.UNSUBSCRIBE,
  97. channel: aChannel
  98. }, aOptions );
  99. }
  100. return lRes ;
  101. },
  102. //:m:*:channelAuth
  103. //:d:en:Authenticates the client at a certain channel to publish messages.
  104. //:a:en::aChannel:String:The id of the server side data channel.
  105. //:a:en::aAccessKey:String:Access key configured for the channel.
  106. //:a:en::aSecretKey:String:Secret key configured for the channel.
  107. //:r:*:::void:none
  108. // TODO: introduce OnResponse here too to get notified on error or success.
  109. channelAuth: function( aChannel, aAccessKey, aSecretKey, aOptions ) {
  110. var lRes = this.checkConnected();
  111. if( 0 == lRes.code ) {
  112. this.sendToken({
  113. ns: jws.ChannelPlugIn.NS,
  114. type: jws.ChannelPlugIn.AUTHORIZE,
  115. channel: aChannel,
  116. // login: this.getUsername(),
  117. accessKey: aAccessKey,
  118. secretKey: aSecretKey
  119. }, aOptions );
  120. }
  121. return lRes;
  122. },
  123. //:m:*:channelPublish
  124. //:d:en:Sends a string message to the given channel on the server.
  125. //:d:en:The client needs to be authenticated against the server and the
  126. //:d:en:channel to publish data. All clients that subscribed to the channel
  127. //:d:en:will receive the message.
  128. //:a:en::aChannel:String:The id of the server side data channel.
  129. //:a:en::aData:String:String (text) to be sent to the server side data channel.
  130. //:r:*:::void:none
  131. // TODO: introduce OnResponse here too to get noticed on error or success.
  132. channelPublishString: function( aChannel, aString, aOptions ) {
  133. var lRes = this.checkConnected();
  134. if( 0 == lRes.code ) {
  135. this.sendToken({
  136. ns: jws.ChannelPlugIn.NS,
  137. type: jws.ChannelPlugIn.PUBLISH,
  138. channel: aChannel,
  139. data: aString
  140. }, aOptions );
  141. }
  142. return lRes;
  143. },
  144. //:m:*:channelPublish
  145. //:d:en:Sends a combined string (id) and map message to the given channel _
  146. //:d:en:on the server. The id can be used to identify the map type/content.
  147. //:d:en:The client needs to be authenticated against the server and the
  148. //:d:en:channel to publish data. All clients that subscribed to the channel
  149. //:d:en:will receive the message.
  150. //:a:en::aChannel:String:The id of the server side data channel.
  151. //:a:en::aData:String:String (text) to be sent to the server side data channel.
  152. //:r:*:::void:none
  153. channelPublish: function( aChannel, aData, aMap, aOptions ) {
  154. var lRes = this.checkConnected();
  155. if( 0 == lRes.code ) {
  156. this.sendToken({
  157. ns: jws.ChannelPlugIn.NS,
  158. type: jws.ChannelPlugIn.PUBLISH,
  159. channel: aChannel,
  160. data: aData,
  161. map: aMap
  162. }, aOptions );
  163. }
  164. return lRes;
  165. },
  166. //:m:*:channelPublishMap
  167. //:d:en:Sends a map message to the given channel on the server.
  168. //:d:en:The client needs to be authenticated against the server and the
  169. //:d:en:channel to publish data. All clients that subscribed to the channel
  170. //:d:en:will receive the message.
  171. //:a:en::aChannel:String:The id of the server side data channel.
  172. //:a:en::aMap:Map:Data object to be sent to the server side data channel.
  173. //:r:*:::void:none
  174. // TODO: introduce OnResponse here too to get noticed on error or success.
  175. channelPublishMap: function( aChannel, aMap, aOptions ) {
  176. var lRes = this.checkConnected();
  177. if( 0 == lRes.code ) {
  178. this.sendToken({
  179. ns: jws.ChannelPlugIn.NS,
  180. type: jws.ChannelPlugIn.PUBLISH,
  181. channel: aChannel,
  182. map: aMap
  183. }, aOptions );
  184. }
  185. return lRes;
  186. },
  187. //:m:*:channelCreate
  188. //:d:en:Creates a new channel on the server. If a channel with the given _
  189. //:d:en:channel-id already exists the create channel request is rejected. _
  190. //:d:en:A private channel requires an access key, if this is not provided _
  191. //:d:en:for a private channel the request is rejected. For public channel _
  192. //:d:en:the access key is optional.
  193. //:a:en::aChannel:String:The id of the server side data channel.
  194. //:a:en::aName:String:The name (human readably) of the channel.
  195. //:r:*:::void:none
  196. channelCreate: function( aId, aName, aOptions ) {
  197. var lRes = this.checkConnected();
  198. if( 0 == lRes.code ) {
  199. var lIsPrivate = false;
  200. var lIsSystem = false;
  201. var lAccessKey = null;
  202. var lSecretKey = null;
  203. var lOwner = null;
  204. var lPassword = null;
  205. if( aOptions ) {
  206. if( aOptions.isPrivate != undefined ) {
  207. lIsPrivate = aOptions.isPrivate;
  208. }
  209. if( aOptions.isSystem != undefined ) {
  210. lIsSystem = aOptions.isSystem;
  211. }
  212. if( aOptions.accessKey != undefined ) {
  213. lAccessKey = aOptions.accessKey;
  214. }
  215. if( aOptions.secretKey != undefined ) {
  216. lSecretKey = aOptions.secretKey;
  217. }
  218. if( aOptions.owner != undefined ) {
  219. lOwner = aOptions.owner;
  220. }
  221. if( aOptions.password != undefined ) {
  222. lPassword = aOptions.password;
  223. }
  224. }
  225. this.sendToken({
  226. ns: jws.ChannelPlugIn.NS,
  227. type: jws.ChannelPlugIn.CREATE_CHANNEL,
  228. channel: aId,
  229. name: aName,
  230. isPrivate: lIsPrivate,
  231. isSystem: lIsSystem,
  232. accessKey: lAccessKey,
  233. secretKey: lSecretKey,
  234. owner: lOwner,
  235. password: lPassword
  236. }, aOptions );
  237. }
  238. return lRes;
  239. },
  240. //:m:*:channelRemove
  241. //:d:en:Removes a (non-system) channel on the server. Only the owner of _
  242. //:d:en:channel can remove a channel. If a accessKey/secretKey pair is _
  243. //:d:en:defined for a channel this needs to be passed as well, otherwise _
  244. //:d:en:the remove request is rejected.
  245. //:a:en::aChannel:String:The id of the server side data channel.
  246. //:r:*:::void:none
  247. channelRemove: function( aId, aOptions ) {
  248. var lRes = this.checkConnected();
  249. if( 0 == lRes.code ) {
  250. var lAccessKey = null;
  251. var lSecretKey = null;
  252. var lOwner = null;
  253. var lPassword = null;
  254. if( aOptions ) {
  255. if( aOptions.accessKey != undefined ) {
  256. lAccessKey = aOptions.accessKey;
  257. }
  258. if( aOptions.secretKey != undefined ) {
  259. lSecretKey = aOptions.secretKey;
  260. }
  261. if( aOptions.owner != undefined ) {
  262. lOwner = aOptions.owner;
  263. }
  264. if( aOptions.password != undefined ) {
  265. lPassword = aOptions.password;
  266. }
  267. }
  268. this.sendToken({
  269. ns: jws.ChannelPlugIn.NS,
  270. type: jws.ChannelPlugIn.REMOVE_CHANNEL,
  271. channel: aId,
  272. accessKey: lAccessKey,
  273. secretKey: lSecretKey,
  274. owner: lOwner,
  275. password: lPassword
  276. }, aOptions );
  277. }
  278. return lRes;
  279. },
  280. //:m:*:channelGetSubscribers
  281. //:d:en:Returns all channels to which the current client currently has
  282. //:d:en:subscribed to. This also includes private channels. The owners of
  283. //:d:en:the channels are not returned due to security reasons.
  284. //:a:en::aChannel:String:The id of the server side data channel.
  285. //:a:en::aAccessKey:String:Access Key for the channel (required for private channels, optional for public channels).
  286. //:r:*:::void:none
  287. channelGetSubscribers: function( aChannel, aAccessKey, aOptions ) {
  288. var lRes = this.checkConnected();
  289. if( 0 == lRes.code ) {
  290. this.sendToken({
  291. ns: jws.ChannelPlugIn.NS,
  292. type: jws.ChannelPlugIn.GET_SUBSCRIBERS,
  293. channel: aChannel,
  294. accessKey: aAccessKey
  295. }, aOptions );
  296. }
  297. return lRes;
  298. },
  299. //:m:*:channelGetSubscriptions
  300. //:d:en:Returns all channels to which the current client currently has
  301. //:d:en:subscribed to. This also includes private channels. The owners of
  302. //:d:en:the channels are not returned due to security reasons.
  303. //:a:en:::none
  304. //:r:*:::void:none
  305. channelGetSubscriptions: function( aOptions ) {
  306. var lRes = this.checkConnected();
  307. if( 0 == lRes.code ) {
  308. this.sendToken({
  309. ns: jws.ChannelPlugIn.NS,
  310. type: jws.ChannelPlugIn.GET_SUBSCRIPTIONS
  311. }, aOptions );
  312. }
  313. return lRes;
  314. },
  315. //:m:*:channelGetIds
  316. //:d:en:Tries to obtain all ids of the public channels
  317. //:a:en:::none
  318. //:r:*:::void:none
  319. channelGetIds: function( aOptions ) {
  320. var lRes = this.checkConnected();
  321. if( 0 == lRes.code ) {
  322. this.sendToken({
  323. ns: jws.ChannelPlugIn.NS,
  324. type: jws.ChannelPlugIn.GET_CHANNELS
  325. }, aOptions );
  326. }
  327. return lRes;
  328. },
  329. setChannelCallbacks: function( aListeners ) {
  330. if( !aListeners ) {
  331. aListeners = {};
  332. }
  333. if( aListeners.OnChannelCreated !== undefined ) {
  334. this.fOnChannelCreated = aListeners.OnChannelCreated;
  335. }
  336. if( aListeners.OnChannelsReceived !== undefined ) {
  337. this.fOnChannelsReceived = aListeners.OnChannelsReceived;
  338. }
  339. if( aListeners.OnChannelRemoved !== undefined ) {
  340. this.fOnChannelRemoved = aListeners.OnChannelRemoved;
  341. }
  342. }
  343. };
  344. // add the ChannelPlugIn PlugIn into the jWebSocketTokenClient class
  345. jws.oop.addPlugIn( jws.jWebSocketTokenClient, jws.ChannelPlugIn );