Persistent connections between Flash client and Ja

2020-07-30 15:01发布

问题:

I'm new to Flash. I'm planning to create a game client in Flash (running in browser) that needs to talk to a server written in Java. Connection between client and server needs to be persistent.

I'm aware of XMLSocket - is that the only way to go? Any recommendations?

Thanks!

回答1:

There's a Socket class, which lets you use real TCP when talking to a server. Downside - you'll have to implement the protocol yourself (that's implementing HTTP client in most cases. Maybe somebody already did it)



回答2:

Are you just going to create the game for leaning and fun? Otherwise I would recommend using an existing game server, or at least investigate the benefits and drawbacks of that option.

I've used SmartfoxServer (http://www.smartfoxserver.com) in a couple of projects, it's not perfect but it takes a lot of your issues away (and you can write extensions in java if you want). I do think ElectroServer http://www.electro-server.com/ is similar, and you can also have a look at the open source server Red5 http://osflash.org/red5

Besides sending stuff in xml you can also investigate if you can use AMF to send data, it might be smaller (as in less bits).



回答3:

Make sure you have a security policy file available on the server. Look at this SO question/answer for more info.



回答4:

I've been experimenting with Socket communication between Flash and Java. One advantage of Socket over XMLSocket is bandwidth, because you can write binary data. So for example, you can send a single entity's position as:

writeShort(entity.id); writeFloat(entity.x); writeFloat(entity.y);

Which is just 10 bytes.

Java supports similar primitive read/write functions with either DataStreams or ByteBuffers.

Actually, the server side of things is more complicated than the Flash side, because you have to choose between the old io Java sockets and the new io methods. The new io methods have much better performance, but are more complicated and apparently filled with gotchas.