Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Storing Client State in Applets  (Read 5085 times)
0 Members and 1 Guest are viewing this topic.
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Posted 2008-06-04 09:26:38 »

I want to store my client state in cookies in the browser for Putty. There didn't seem to be a way to set cookies client side so I wrote a PHP script that the client calls the return from which sets the cookie, this isn't going to work in the future (some places I'd like to host can't support PHP). So, is there a way to set cookies client side without an external URL?

Kev

Online Riven
Showcase Moderator

JGO Overlord


Medals: 611
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-06-04 09:50:46 »

applet.showDocument("javascript:myFunction();");

Where myFunction() does what you want.

--- this does NOT work in Opera --- it doesn't understand the 'javascript:' protocol


A heck of a lot more stable is this:
applet.showDocument("javascript:eval(hexToStr('"+strToHex("myFunction();")+"'));");


You have to implement strToHex and hexToStr yourself though.




The workaround in Opera is to have this iframe in your page...
applet.showDocument("http://mysite.com/runscript.php?eval_hex="+strToHex("myFunction();"), "myIframeTarget");
where runscript.php reads the argument, decodes it with hexToStr and writes <script ...> </script> around it.


I use this to communicate between a webpage and an applet (both ways) without LiveConnect (which is buggy in FF)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #2 - Posted 2008-06-04 12:13:12 »

what about using
1  
2  
JSObject window = JSObject.getWindow(appletinstance); 
window.eval("myFunction();");
?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2008-06-04 12:35:07 »

Been meaning to ask about this JSObject stuff, is that compatible everywhere? Seemed to be borked on my IE.

Kev

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2008-06-04 13:44:04 »

Is this any help at all?

http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/cookie_support.html

I'm kinda wondering how to best do this as well..

Play Minecraft!
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2008-06-04 14:07:40 »

Yeah, went through that. The cookie management thing requires signed JARs - hence the PHP hack.

Kev

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #6 - Posted 2008-06-04 14:17:29 »

Isn't that just the two last examples (using CookieHandler)? The first two seems to do things manually via URLConnections.

Play Minecraft!
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2008-06-04 15:05:26 »

I can't see how the first two actually let you store and retrieve values as cookies though. The server has to be set up to set the cookie on the response doesn't it (which is where my PHP came in)?

Kev

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2008-06-04 15:36:18 »

Oh, I thought it was some kind of standard HTTP stuff. That's a shame.

Flash (and Silverlight) has some really nice limited local storage stuff that's even cross platform so you can get the same "cookies" on the same computer regardless of what browser you use. Java needs something like that.

Play Minecraft!
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2008-06-04 15:53:06 »

Yeah, I was kinda hoping the Java Preferences API was going to be it, but I seem to remember that was security manager restricted aswell.

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #10 - Posted 2008-06-04 16:07:53 »

Actually, if that JSObject is supported everywhere couldn't we use:

1  
2  
3  
4  
5  
6  
// get cookies
JSObject document = (JSObject) window.getMember("document");
String load = (String) document.getMember("cookie");

// set cookies
document.setMember("cookie", "x=y; x2=y2");


Kev

Offline Mr_Light

Senior Member




shiny.


« Reply #11 - Posted 2008-06-04 16:16:50 »

Actually, if that JSObject is supported everywhere couldn't we use:

It seems it's there since 1.3
http://java.sun.com/products/plugin/1.3/docs/jsobject.html

The problem isn't the java version, but the quality of the implementation might suck?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Online Riven
Showcase Moderator

JGO Overlord


Medals: 611
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2008-06-04 19:19:58 »

This might sound arrogant, but I don't understand why you'd look any further than my solution...

It works for me, in all browsers, even Safari.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline brackeen

Junior Member





« Reply #13 - Posted 2008-06-04 19:22:20 »

Watch out for the FF bug in 1.6.0_03 - 1.6.0_09 (fixed in update 10), where using LiveConnect causes problems: http://bugs.sun.com/view_bug.do?bug_id=6669818

Web browsers may have the following limitations for cookies (according to RFC 2109 section 6.3):

* 300 cookies total
* 20 cookies per domain (per site, not per page)
* 4,096 bytes per cookie (name and value combined)
* Additionally, Internet Explorer allows only 4,096 bytes per domain (!)

Also of course cookies can have limitations on the characters they can use. An easy way around that is to encode data as Base64.

Since there are only 20 cookies per domain, what I do is only store one cookie per game, and that cookie is a Base64-encoded gzipped byte array of tons of data (name, previous scores, sound on/off, etc).

In 6u10, the JNLP API can be used (PersistenceService).


Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #14 - Posted 2008-06-04 20:07:19 »

This might sound arrogant, but I don't understand why you'd look any further than my solution...

It works for me, in all browsers, even Safari.

Maybe I read it wrong - the first bit you said didn't work in Opera. The second bit (Opera workaround) relies on PHP (which isn't available on a site where I'm going to host - it also can't link out to somewhere that does use PHP).

Thanks for the details brackeen - how do you go about setting cookies atm?

Kev

Online Riven
Showcase Moderator

JGO Overlord


Medals: 611
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2008-06-04 20:16:29 »

Well, it *can* be implemented by PHP...


In javascript you can figure out your own URI using window.location.href (including the part after the '?') so you can put your decoder+eval(...) in your body.onLoad

Problem solved.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline brackeen

Junior Member





« Reply #16 - Posted 2008-06-04 20:19:42 »

Thanks for the details brackeen - how do you go about setting cookies atm?
Using JSObject (via reflection), but Riven's idea looks interesting for setting cookies... it would be a good workaround for the FF LiveConnect bug.

Riven, how to you get cookies via this method?
Offline kevglass

JGO Kernel


Medals: 85
Projects: 22


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #17 - Posted 2008-06-04 20:27:52 »

Cheers. So I could use that method in all cases?

Kev

Online Riven
Showcase Moderator

JGO Overlord


Medals: 611
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2008-06-04 20:44:26 »

Riven, how to you get cookies via this method?


Read document.cookie.
Use DOM to insert it into the <applet><param name="myCookie" value="insertHere"/></applet>
poll the applet.getParameter("myCookie") in the Applet for change.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (73 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!