Guest User

Untitled

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. /*
  2. This should be split in a pure OrionNodeRiak socket io client
  3. and a set of callbacks that can be used by a data source
  4.  
  5. */
  6.  
  7. function dict () {
  8. var keys = [];
  9. var values = [];
  10.  
  11. return {
  12. get: function (key) {
  13. return values[keys.indexOf(key)]
  14. },
  15.  
  16. set: function (key, value) {
  17. var i = keys.indexOf(key);
  18. if (i === -1) {
  19. i = keys.length;
  20. }
  21. keys[i] = key;
  22. values[i] = value;
  23. },
  24.  
  25. del: function (key) {
  26. var i = keys.indexOf(key);
  27. keys.splice(i, 1);
  28. values.splice(i, 1);
  29. },
  30.  
  31. keys: function () {
  32. return keys.slice();
  33. },
  34.  
  35. values: function () {
  36. return values.slice();
  37. },
  38. };
  39. }
  40.  
  41. var wstest = {
  42.  
  43. ws: null,
  44.  
  45. user: '', // store the current authenticated user
  46.  
  47. sessionKey: '', // store sessionKey
  48.  
  49. isConnected: false,
  50.  
  51. start: function(){
  52. this.ws = new WebSocket('ws://localhost:8080/socket.io/websocket');
  53. this.ws.onopen = this.createOnOpenHandler();
  54. this.ws.onmessage = this.createOnMessageHandler();
  55. this.ws.onerror = this.createOnErrorHandler();
  56. this.ws.onclose = this.createOnCloseHandler();
  57.  
  58. // all these handlers call the on* functions on this object
  59. },
  60.  
  61. createFeederData: function(){
  62. this.send( {
  63. createRecord: {
  64. bucket: "feeder_data",
  65. record: {
  66. season: "2008-2009",
  67. region: "Southeastern US",
  68. common_name: "Eastern Towhee",
  69. rank: 17,
  70. percentage_of_feeders_visited: 49.60,
  71. mean_group_size_when_seen: 1.49,
  72. feederwatch_abundance_index: 0.25
  73. },
  74. returnData: { birdkey: "Eastern Towhee" }
  75. }
  76. });
  77. this.send( {
  78. createRecord: {
  79. bucket: "feeder_data",
  80. record: {
  81. season: "2008-2009",
  82. region: "Southeastern US",
  83. common_name: "House Finch",
  84. rank: 8,
  85. percentage_of_feeders_visited: 74.17,
  86. mean_group_size_when_seen: 3.38,
  87. feederwatch_abundance_index: 1.32
  88. },
  89. returnData: { birdkey: "House Finch" }
  90. }
  91. });
  92. this.fetchRequest("feeder_data");
  93. },
  94.  
  95. fetchFeederData: function(){
  96. //function to test some stuff
  97. //this.authRequest("test","test"); // automatic
  98. this.fetchRequest("feeder_data");
  99. },
  100.  
  101. test: function(){
  102. //function to test some stuff
  103. this.authRequest("test","test"); // automatic
  104. //this.createRequest("bird",{ genus: "Turdus", species:"migratorius", commonname: "American Robin"});
  105. //this.fetchRequest("bird");
  106. //this.refreshRequest("teacher","2");
  107. //this.createRequest("teacher",{ firstname: "Martijn", inbetween:"", lastname: "Alsters"});
  108. },
  109.  
  110. send: function(val){
  111. console.log('Send function called on wstest');
  112. if(this.ws && val){
  113. var msg = JSON.stringify(val);
  114. console.log('Trying to send message: ' + msg);
  115. return this.ws.send(msg);
  116. }
  117. else return false;
  118. },
  119.  
  120. createOnOpenHandler: function(){ // to create an onOpen callback
  121. var me = this;
  122. return function(event){
  123. me.isConnected = true;
  124. me.test();
  125. return;
  126. };
  127. },
  128.  
  129. createOnMessageHandler: function(){
  130. var me = this;
  131. return function(event){
  132. // first of all: try to parse the data
  133. if(event.data){
  134. console.log("data in event: " + event.data);
  135. var messages = JSON.parse(event.data);
  136. if(messages){
  137. // check if messages is an array, if not, make one
  138. var data = (messages instanceof Array)? messages: [messages];
  139. for(var i=0, len = data.length;i<len;i++){
  140. var message = data[i];
  141. //console.log('processing message: ' + JSON.stringify(message));
  142. // use special handlers for special messages, no clue how to do this with switch()
  143. if(message.authSuccess){
  144. me.onAuthSuccess.call(me,message.authSuccess);
  145. return;
  146. }
  147. if(message.authError){
  148. me.onAuthError.call(me,message.authError);
  149. return;
  150. }
  151. if(message.logoutSuccess){
  152. me.onLogoutSuccess.call(me,message.logoutSuccess);
  153. return;
  154. }
  155. me.onDataMessage.call(me,message); //default
  156. } // end for
  157. }
  158. else console.log("Received information from the server that couldn't be parsed");
  159. } // otherwise ignore
  160.  
  161. // there are a number of messages to intercept, such as authSuccess, authError
  162.  
  163. //console.log('MyonMessage: ' + event.toString());
  164. //console.log('MyonMessage: data in message: ' + event.data);
  165. };
  166. },
  167.  
  168. createOnErrorHandler: function(){
  169. var me = this;
  170. return function(event){
  171. console.log('MyonError: ' + event.toString());
  172. };
  173. },
  174.  
  175. createOnCloseHandler: function(event){
  176. var me = this;
  177. return function(event){
  178. console.log('MyonClose: ' + event.toString());
  179. // don't throw away existing user and session information
  180. me.isConnected = false;
  181. };
  182. },
  183.  
  184. authRequest: function(user,passwd,passwdIsMD5){
  185. if(this.isConnected){
  186. var baseRequest = {auth:{ user: user, passwd: passwd, passwdIsMD5: passwdIsMD5}};
  187. if(this.sessionKey) baseRequest.auth.sessionKey = this.sessionKey; // resume the session if possible
  188. this.send(baseRequest);
  189. }
  190. else console.log('Cannot send an authentication request because there is no active connection');
  191. },
  192.  
  193. refreshRequest: function(bucket,key){
  194. this.send({ refreshRecord: { bucket: bucket, key: key}});
  195. },
  196.  
  197. createRequest: function(bucket,data){
  198. this.send({ createRecord: { bucket: bucket, record: data}});
  199. },
  200.  
  201. fetchRequest: function(bucket){
  202. this.send({ fetch: { bucket: bucket}});
  203. },
  204.  
  205. onAuthSuccess: function(data){
  206. // function called when authorisation has been completed successfully
  207. console.log('onAuthSuccess called on ' + this);
  208. this.user = data.user;
  209. this.sessionKey = data.sessionKey;
  210. this.createFeederData();
  211. },
  212.  
  213. onAuthError: function(data){
  214. // function called when authorisation has gone awry for some reason
  215. var errorMsg = data.authError.errorMsg;
  216. alert('Authentication error: ' + errorMsg);
  217. console.log('Authentication error: ' + errorMsg);
  218. },
  219.  
  220. onLogoutSuccess: function(data){
  221. // function called when logout has been successfull
  222. // remove user and session information
  223. this.user = undefined;
  224. this.sessionKey = undefined;
  225. },
  226.  
  227. /*
  228. DATA requests:
  229. { refreshRecord: { bucket: '', key: ''}}
  230. { fetch: { bucket: '', conditions: '', returnData: {} }}
  231. { createRecord: { bucket: '', record: {}, returnData: {} }}
  232. { updateRecord: { bucket: '', key: '', record: {}, returnData: {} }}
  233. { deleteRecord: { bucket: '', key: '', returnData: {} }}
  234.  
  235. // most properties are self explanatory, but returnData needs some explanation on its own.
  236. // return data is an object that can be delivered along side the request and which is
  237. // returned by the server in the answer to that request. This helps the client side identifying
  238. // what request was answered exactly.
  239.  
  240. // returned by the server as answer to a client request
  241. { fetchResult: { bucket: '', records: [], returnData: {} }}
  242. { createRecordResult: {}, returnData: {} }
  243. { updateRecordResult: {}, returnData: {} }
  244. { deleteRecordResult: {}, returnData: {} }
  245. { refreshRecordResult: {}, returnData: {} }
  246. */
  247.  
  248. onDataMessage: function(data){
  249. // function called when a data message has arrived
  250. // this is the part where interaction with the store comes into play
  251. // let's create handlers for every type of action
  252. if(data.createRecord) this.onPushedCreateRecord(data);
  253. if(data.updateRecord) this.onPushedUpdateRecord(data);
  254. if(data.deleteRecord) this.onPushedDeleteRecord(data);
  255. if(data.fetchResult) this.onFetchResult(data);
  256. if(data.createRecordResult) this.onCreateRecordResult(data);
  257. if(data.updateRecordResult) this.onUpdateRecordResult(data);
  258. if(data.deleteRecordResult) this.onDeleteRecordResult(data);
  259. if(data.refreshRecordResult) this.onRefreshRecordResult(data);
  260. },
  261.  
  262. onPushedCreateRecord: function(data){
  263. // function to process the creation of a record in the store with the pushed data by the server
  264. // used when a different user creates a record of which the current user should know
  265. },
  266.  
  267. onPushedUpdateRecord: function(data){
  268. // function to update a change in a record in the store with pushed data by the server
  269. // used when a different user updates a record of which the current user should know
  270.  
  271. },
  272.  
  273. onPushedDeleteRecord: function(data){
  274. // function to delete a record in the store with pushed data by the server
  275. // used when a different user deletes a record of which the current user should know
  276.  
  277. },
  278.  
  279. onFetchResult: function(data){
  280. // function to process the fetch data returned from a fetch call
  281. },
  282.  
  283. onCreateRecordResult: function(data){
  284. console.log('return of onCreateRecordResult' + data.toString);
  285. // function to process the data from the server when a createRecord call has been made to the server
  286. //if (data.bucket === 'abbreviation') {
  287. // this.abbreviationsHandler.set(data.returnData, data.key);
  288. //} else if (data.bucket === 'feeder_data') {
  289. // this.feederDataHandler.set(data.returnData, data.key);
  290. //}
  291. },
  292.  
  293. onUpdateRecordResult: function(data){
  294. // function to process the data from the server when an updateRecord call has been made to the server
  295. },
  296.  
  297. onRefreshRecordResult: function(data){
  298. // function to process the data from the server when a refreshRecord call has been made to the server
  299. },
  300.  
  301. onDeleteRecordResult: function(data){
  302. // function to process the data from the server when a deleteRecord call has been made to the server
  303. }
  304. };
Add Comment
Please, Sign In to add comment