Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This should be split in a pure OrionNodeRiak socket io client
- and a set of callbacks that can be used by a data source
- */
- function dict () {
- var keys = [];
- var values = [];
- return {
- get: function (key) {
- return values[keys.indexOf(key)]
- },
- set: function (key, value) {
- var i = keys.indexOf(key);
- if (i === -1) {
- i = keys.length;
- }
- keys[i] = key;
- values[i] = value;
- },
- del: function (key) {
- var i = keys.indexOf(key);
- keys.splice(i, 1);
- values.splice(i, 1);
- },
- keys: function () {
- return keys.slice();
- },
- values: function () {
- return values.slice();
- },
- };
- }
- var wstest = {
- ws: null,
- user: '', // store the current authenticated user
- sessionKey: '', // store sessionKey
- isConnected: false,
- start: function(){
- this.ws = new WebSocket('ws://localhost:8080/socket.io/websocket');
- this.ws.onopen = this.createOnOpenHandler();
- this.ws.onmessage = this.createOnMessageHandler();
- this.ws.onerror = this.createOnErrorHandler();
- this.ws.onclose = this.createOnCloseHandler();
- // all these handlers call the on* functions on this object
- },
- createFeederData: function(){
- this.send( {
- createRecord: {
- bucket: "feeder_data",
- record: {
- season: "2008-2009",
- region: "Southeastern US",
- common_name: "Eastern Towhee",
- rank: 17,
- percentage_of_feeders_visited: 49.60,
- mean_group_size_when_seen: 1.49,
- feederwatch_abundance_index: 0.25
- },
- returnData: { birdkey: "Eastern Towhee" }
- }
- });
- this.send( {
- createRecord: {
- bucket: "feeder_data",
- record: {
- season: "2008-2009",
- region: "Southeastern US",
- common_name: "House Finch",
- rank: 8,
- percentage_of_feeders_visited: 74.17,
- mean_group_size_when_seen: 3.38,
- feederwatch_abundance_index: 1.32
- },
- returnData: { birdkey: "House Finch" }
- }
- });
- this.fetchRequest("feeder_data");
- },
- fetchFeederData: function(){
- //function to test some stuff
- //this.authRequest("test","test"); // automatic
- this.fetchRequest("feeder_data");
- },
- test: function(){
- //function to test some stuff
- this.authRequest("test","test"); // automatic
- //this.createRequest("bird",{ genus: "Turdus", species:"migratorius", commonname: "American Robin"});
- //this.fetchRequest("bird");
- //this.refreshRequest("teacher","2");
- //this.createRequest("teacher",{ firstname: "Martijn", inbetween:"", lastname: "Alsters"});
- },
- send: function(val){
- console.log('Send function called on wstest');
- if(this.ws && val){
- var msg = JSON.stringify(val);
- console.log('Trying to send message: ' + msg);
- return this.ws.send(msg);
- }
- else return false;
- },
- createOnOpenHandler: function(){ // to create an onOpen callback
- var me = this;
- return function(event){
- me.isConnected = true;
- me.test();
- return;
- };
- },
- createOnMessageHandler: function(){
- var me = this;
- return function(event){
- // first of all: try to parse the data
- if(event.data){
- console.log("data in event: " + event.data);
- var messages = JSON.parse(event.data);
- if(messages){
- // check if messages is an array, if not, make one
- var data = (messages instanceof Array)? messages: [messages];
- for(var i=0, len = data.length;i<len;i++){
- var message = data[i];
- //console.log('processing message: ' + JSON.stringify(message));
- // use special handlers for special messages, no clue how to do this with switch()
- if(message.authSuccess){
- me.onAuthSuccess.call(me,message.authSuccess);
- return;
- }
- if(message.authError){
- me.onAuthError.call(me,message.authError);
- return;
- }
- if(message.logoutSuccess){
- me.onLogoutSuccess.call(me,message.logoutSuccess);
- return;
- }
- me.onDataMessage.call(me,message); //default
- } // end for
- }
- else console.log("Received information from the server that couldn't be parsed");
- } // otherwise ignore
- // there are a number of messages to intercept, such as authSuccess, authError
- //console.log('MyonMessage: ' + event.toString());
- //console.log('MyonMessage: data in message: ' + event.data);
- };
- },
- createOnErrorHandler: function(){
- var me = this;
- return function(event){
- console.log('MyonError: ' + event.toString());
- };
- },
- createOnCloseHandler: function(event){
- var me = this;
- return function(event){
- console.log('MyonClose: ' + event.toString());
- // don't throw away existing user and session information
- me.isConnected = false;
- };
- },
- authRequest: function(user,passwd,passwdIsMD5){
- if(this.isConnected){
- var baseRequest = {auth:{ user: user, passwd: passwd, passwdIsMD5: passwdIsMD5}};
- if(this.sessionKey) baseRequest.auth.sessionKey = this.sessionKey; // resume the session if possible
- this.send(baseRequest);
- }
- else console.log('Cannot send an authentication request because there is no active connection');
- },
- refreshRequest: function(bucket,key){
- this.send({ refreshRecord: { bucket: bucket, key: key}});
- },
- createRequest: function(bucket,data){
- this.send({ createRecord: { bucket: bucket, record: data}});
- },
- fetchRequest: function(bucket){
- this.send({ fetch: { bucket: bucket}});
- },
- onAuthSuccess: function(data){
- // function called when authorisation has been completed successfully
- console.log('onAuthSuccess called on ' + this);
- this.user = data.user;
- this.sessionKey = data.sessionKey;
- this.createFeederData();
- },
- onAuthError: function(data){
- // function called when authorisation has gone awry for some reason
- var errorMsg = data.authError.errorMsg;
- alert('Authentication error: ' + errorMsg);
- console.log('Authentication error: ' + errorMsg);
- },
- onLogoutSuccess: function(data){
- // function called when logout has been successfull
- // remove user and session information
- this.user = undefined;
- this.sessionKey = undefined;
- },
- /*
- DATA requests:
- { refreshRecord: { bucket: '', key: ''}}
- { fetch: { bucket: '', conditions: '', returnData: {} }}
- { createRecord: { bucket: '', record: {}, returnData: {} }}
- { updateRecord: { bucket: '', key: '', record: {}, returnData: {} }}
- { deleteRecord: { bucket: '', key: '', returnData: {} }}
- // most properties are self explanatory, but returnData needs some explanation on its own.
- // return data is an object that can be delivered along side the request and which is
- // returned by the server in the answer to that request. This helps the client side identifying
- // what request was answered exactly.
- // returned by the server as answer to a client request
- { fetchResult: { bucket: '', records: [], returnData: {} }}
- { createRecordResult: {}, returnData: {} }
- { updateRecordResult: {}, returnData: {} }
- { deleteRecordResult: {}, returnData: {} }
- { refreshRecordResult: {}, returnData: {} }
- */
- onDataMessage: function(data){
- // function called when a data message has arrived
- // this is the part where interaction with the store comes into play
- // let's create handlers for every type of action
- if(data.createRecord) this.onPushedCreateRecord(data);
- if(data.updateRecord) this.onPushedUpdateRecord(data);
- if(data.deleteRecord) this.onPushedDeleteRecord(data);
- if(data.fetchResult) this.onFetchResult(data);
- if(data.createRecordResult) this.onCreateRecordResult(data);
- if(data.updateRecordResult) this.onUpdateRecordResult(data);
- if(data.deleteRecordResult) this.onDeleteRecordResult(data);
- if(data.refreshRecordResult) this.onRefreshRecordResult(data);
- },
- onPushedCreateRecord: function(data){
- // function to process the creation of a record in the store with the pushed data by the server
- // used when a different user creates a record of which the current user should know
- },
- onPushedUpdateRecord: function(data){
- // function to update a change in a record in the store with pushed data by the server
- // used when a different user updates a record of which the current user should know
- },
- onPushedDeleteRecord: function(data){
- // function to delete a record in the store with pushed data by the server
- // used when a different user deletes a record of which the current user should know
- },
- onFetchResult: function(data){
- // function to process the fetch data returned from a fetch call
- },
- onCreateRecordResult: function(data){
- console.log('return of onCreateRecordResult' + data.toString);
- // function to process the data from the server when a createRecord call has been made to the server
- //if (data.bucket === 'abbreviation') {
- // this.abbreviationsHandler.set(data.returnData, data.key);
- //} else if (data.bucket === 'feeder_data') {
- // this.feederDataHandler.set(data.returnData, data.key);
- //}
- },
- onUpdateRecordResult: function(data){
- // function to process the data from the server when an updateRecord call has been made to the server
- },
- onRefreshRecordResult: function(data){
- // function to process the data from the server when a refreshRecord call has been made to the server
- },
- onDeleteRecordResult: function(data){
- // function to process the data from the server when a deleteRecord call has been made to the server
- }
- };
Add Comment
Please, Sign In to add comment