I have this website, I'll be happy if I could store the game progress of the user, and then retrieve it to their respective users when they login. Is there any way to do this? Maybe storing the data to a DB. I was reading on Internet and I found something about Shared Objects. I don't know if it is useful, I hope it will.. Thanks in advance and sorry for my English.
EDIT: I publish external games, I didn't develop them.
You can use a SharedObject to store data locally, on the client side:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
This is very easy to implement:
var user_data_so:SharedObject = SharedObject.getLocal("myUserData");
user_data_so.data.name = "User Name";
user_data_so.data.score = user_score;
SharedObjects also work with a NetConnection (see getRemote), although I haven't used this method. Other than that, a server-side script in any language you're comfortable with can accept a POST (or GET) from the client:
var req:URLRequest = new URLRequest(user_data_url);
req.data = user_data_xml;
req.contentType = "text/xml";
req.method = URLRequestMethod.POST;
// either this (will ignore server response):
sendToURL(req);
// or (handles status, error, completion):
var url_loader = new URLLoader();
url_loader.addEventListener("complete",completion_handler);
url_loader.addEventListener("ioError",error_handler);
url_loader.addEventListener("securityError",error_handler);
url_loader.addEventListener("httpResponseStatus",status_handler);
url_loader.addEventListener("httpStatus",status_handler);
url_loader.load(req);
Related
I am new to programming, and I heard that some guys on this website are quite angry, but please don't be. I am creating one web app, that has a web page and also makes som ecalculations and works with database (NeDB). I have an index.js
const selects = document.getElementsByClassName("sel");
const arr = ["Yura", "Nairi", "Mher", "Hayko"];
for (let el in selects) {
for (let key in arr) {
selects[el].innerHTML += `<option>${arr[key]}</option>`;
}
}
I have a function which fills the select elements with data from an array.
In other file named: getData.js:
var Datastore = require("nedb");
var users = new Datastore({ filename: "players" });
users.loadDatabase();
const names = [];
users.find({}, function (err, doc) {
for (let key in doc) {
names.push(doc[key].name);
}
});
I have some code that gets data from db and puts it in array. And I need that data to use in the index.js mentioned above, but the problem is that I don't know how to tranfer the data from getData.js to index.js. I have tried module.exports but it is not working, the browser console says that it can't recognize require keyword, I also can't get data directly in index.js because the browse can't recognize the code related to database.
You need to provide a server, which is connected to the Database.
Browser -> Server -> DB
Browser -> Server: Server provides endpoints where the Browser(Client) can fetch data from. https://expressjs.com/en/starter/hello-world.html
Server -> DB: gets the Data out of the Database and can do whatever it want with it. In your case the Data should get provided to the Client.
TODOs
Step 1: set up a server. For example with express.js (google it)
Step 2: learn how to fetch Data from the Browser(Client) AJAX GET are the keywords to google.
Step 3: setup a Database connection from you Server and get your data
Step 4: Do whatever you want with your data.
At first I thought it is a simple method, but them I researched a little bit and realized that I didn't have enough information about how it really works. Now I solved the problem, using promises and templete engine ejs. Thank you all for your time. I appreciate your help)
We are on a SAP HCP. For our application we need to provide a Web API.
We are unsure wheather to use an OData Service or a custom XSJS service implementation for this.
A few things we need to accomplish. All items also need to do a certain user authorization.
Select data from the database
Writing data into the database
Providing API functions that don't perfrom CRUD operations on the database, but for example send data to another server in the background
In my understanding an OData service would only fit the first two items, but does not provide a way to do anything that is not database (CRUD) related. Is this correct?
Are there any best practices for this scenario? Any suggestions and pointings in the right direction are welcome.
XSOData would no be able to perform non CRUD operations. With XSJS you can achieve this. I have done all of the three requirements on an on-premise
system & the same concepts should apply to HCP as well.
Sample code for executing a query & displaying data :
var queryString = "SELECT * FROM Table";
var connection = $.db.getConnection(); // DB Connection
var stmt = connection.prepareStatement(queryString);
var rs = stmt.executeQuery();
var output = "";
while (rs.next()) // Column setting
{
output += ("Col1:"+rs.getString(0)+", Col2:"+rs.getString(1)+";";
}
$.response.setBody(output);
To check for any GET or POST request & request parameters you can use $.request. With these you can perform your CRUD operations
You can refer to the XSJS API Reference for more info
http://help.sap.com/hana/SAP_HANA_XS_JavaScript_API_Reference_en/
For sending data to another server, one way would be by providing a web service from the second server. XSJS can make Web Request to a configured destination so you can POST data to your server. You will need to create a destination file(.xshttpdest) for accessing you server
Some sample Code:
var dest = $.net.http.readDestination("<package path to destination file>", "<destination file name>");
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.POST, "/");
req.contentType = "application/json";
req.setBody("My data");
client.request(req, dest);
var response = client.getResponse();
You can find details on the below link to create a HTTP Destination & send data https://help.hana.ondemand.com/help/frameset.htm?06ca24043b0f4eb78cf39c6bc1d8a8dc.html
Ok guys. I've built a web application with some friends that is somehow complex and took us some time to built. It is entirely built using JavaScript
Now the application has some functions which we don't want to readily share with the world(view-source).
The application works by sending an input to an algorithm and receiving the output from that algorithm and displays it on a canvas. (Thats how all functions work anyway:) )
Okay i don't know much about node.js but from what i've heard its server-side JavaScript. Does that mean that i can transfer the function server-side and using POST-GET to send an input and receive an output? All without having the source code of that particular function readily visible to anyone?
Please don't get started about how i should be more worried about doing things better rather than worrying about the safety of our work. I already know that so consider my question as a complimentary safeguard, at least for the time being.
Obfuscation is not a very concrete option since many developers de-obfuscate code just for fun.
This application is not like a hen with a golden egg and i am not being grandiose about this, but i was just having this question in my mind for some time now so i just shoot it here to know in the future how things work.
Thanks guys!
If you're worried about protecting your ultra sweet super secret codes, you could host your app and use a little something called RPC. Remote. Procedure. Calls.
Check out this little guy
https://github.com/deoxxa/pillion
Easy as 1-2-3 or A-B-C or cake or strippers or whatever else is easy
% npm install pillion burro && echo omg that was so easy
DISCLAIMER
I'm about to reveal a super secret greeting function that says hello to our clients. This is ultra secret intellectual IP properties and should not be shared with anyone on the internets.
You could provide the function calls you need using something like this on the server
// server.js
var net = require("net"),
burro = require("burro"),
pillion = require("pillion");
var server = net.createServer(function(_socket) {
var socket = burro.wrap(_socket),
rpc = new pillion(socket);
rpc.provide("greet", function(name, cb) {
cb("hi there, " + name);
});
});
server.listen(3000);
Then on the client side
// client.js
var net = require("net"),
burro = require("burro"),
pillion = require("pillion");
var _socket = net.connect(3000),
socket = burro.wrap(_socket),
rpc = new pillion(socket);
rpc.callRemote("greet", "friend", function(res) {
console.log(res); // prints "hi there, friend"
});
I'm trying to access an express.js api done with node.js and passport.js. Some methods just work when req.use is available.
I try to log with facebook and twitter from my client app, and I get plain cookie.
Something like connect.sid=jkbsdkjabdkjasdbjkadnaidiadiasdaoidoa
I recreate the client cookie with this method:
BasicClientCookie clientCookie = new BasicClientCookie(Metadata.COOKIE_LOGIN, cookieValue);
clientCookie.setDomain("www.domain.com");
clientCookie.setPath("/");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
clientCookie.setExpiryDate(calendar.getTime());
return clientCookie;
And after that I make Json Requests with this:
HttpUriRequest request = method.createRequest(uri);
request.addHeader("Accept", "application/json");
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
DefaultHttpClient client = new DefaultHttpClient(params);
client.getCookieStore().addCookie(USER_SESSION_COOKIE);
HttpResponse response = client.execute(request);
String serializedJson = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
T fromJson = gsonParser.fromJson(serializedJson, clazz);
I cannot get on express' routes methods the user as usual.
I know that I'm doing something wrong but I don't know what.
Anyone has done a connection betweem an android app and passport.js?
Thank you all!
See this question for a quick insight on how to use OkHttp with a persistent cookie store. Using a HTTP library to access your REST API will make things much easier.
My application receives data from the another server, using API with limited number of requests. Data changing rarely, but may be necessary even after refresh page.
What's the best solution this problem, using cookie or HTML5
WebStorage?
And may be have other way to solve this task?
As much as cross browser compatibility matters, cookie is the only choice rather than web storage.
But the question really depends on what kind of data you are caching?
For what you are trying, cookie and web-storage might not be needed at all.
Cookies are used to store configuration related information, rather than actual data itself.
Web storage supports persistent data storage, similar to cookies but with a greatly enhanced capacity and no information stored in the HTTP request header. [1]
I would rather say, it would be stupid to cache the entire page as cookie or web-storage both. For these purposes, server-side caching options might be the better way.
Update:
Quoting:
data about user activity in some social networks (fb, vk, google+)
Detect the web-storage features, using libraries like mordernizr and if does not exists fall back to cookie method. A simple example
if (Modernizr.localstorage) {
// browser supports local storage
// Use this method
} else {
// browser doesn't support local storage
// Use Cookie Method
}
[1]: http://en.wikipedia.org/wiki/Web_storage
I wrote this lib to solve the same problem:
Cache your data with Javascript using cacheJS
Here are some basic usages
// just add new cache using array as key
cacheJS.set({blogId:1,type:'view'},'<h1>Blog 1</h1>');
cacheJS.set({blogId:1,type:'json'}, jsonData);
// remove cache using key
cacheJS.removeByKey({blogId:1,type:'json'});
// add cache with ttl and contextual key
cacheJS.set({blogId:2,type:'view'},'<h1>Blog 2</h1>', 3600, {author:'hoangnd'});
cacheJS.set({blogId:3,type:'view'},'<h1>Blog 3</h1>', 3600, {author:'hoangnd'});
// remove cache with con textual key
// cache for blog 2 and 3 will be removed
cacheJS.removeByContext({author:'hoangnd'})
Here is an example of caching data from JQuery AJAX. So if you only want to make the call when you don't have the data yet, its really simple. just do this (example). Here we first check if we have the load information (keyed on line, location and shipdate), and only if we dont, we make the AJAX call and put that data into our cache:
var dict = [];
function checkCachedLoadLine(line, location, shipDate, callback) {
var ret = 0;
if(!((line+location+shipDate) in dict)) {
productionLineService.getProductionLoadLine(line, location, shipDate, callback);
}
return dict[line+location+shipDate];
}
...then in the call back write the value to the cache
function callback(data) {
if (!data) {
document.getElementById('htmlid').innerHTML = 'N/A';
} else {
document.getElementById('htmlid').innerHTML = data[0];
dict[data[2]+data[3]+data[4]] = data[0];
}
}