Socket programming vs. WebSocket for real-time multiplayer game in Android - sockets

I'm working on real-time multiplayer game in Android (java) and I narrowed down the my choices for bidirectional connection to WebSocket and Socket programming
My requirements are:
Fast Reaction Time
High-Frequency send and receive with Small Payloads
Stable connection
Secure Connection
Image and audio transfer
In app chats
Base on these requirements, I would like to know which is more suitable.
Thanks.

A WebSocket is a regular TCP socket connection that:
Mainly, it is supported in web browsers, so they can be used from Javascript.
Starts as a HTTP connection, allowing it to easily cross proxies and firewalls, and make use of HTTP mechanisms like cookies and the Origin HTTP header among others. So for example, a Websocket connection will send the cookies that the browser has for that domain.
Uses its own data framing protocol.
Uses its own PING/PONG schema.
If you do not need any of those, you may be alright with just a socket.

Related

Implementing computation on a TCP based game application

We are planning a real-time network game application for mobiles.
After some researches and studying, we decided to use a TCP connection between the server and the clients (mobile devices) by implementing Socket.
As we are writing the protocols, we came across the following question: where should we implement the computation? In the user's device or in the server?
For example, imagine a short game between 5 players which does not require communication until the game ends (hitting the screen as fast as you can or something like that).
At the end of the round, the clients send their score to the server, and the application should display the loser or the winner.
Where should the computation take place? Should the server collect all of the scores, compute the maximum and the minimum and send back the loser and winner? Or should the server send back all the scores to the players, in which their devices will compute the results?
What are the pros and cons of each strategy?
A couple of things here:
1) Do not use a persistent socket for mobile applications. Connectivity is too spotty.
2) Use a higher level protocol, like HTTP. You have no need to use such a low-level protocol. By doing so you are going to end up doing far more work than you need to. Think about if you want to build this into a web-based game, or implement on a device that doesn't allow you to use raw sockets.
HTTP is nice because it is firewall friendly, most firewalls already let HTTP on standard ports through. This is why it is so popular these days.
3) You should think very skeptically about the devices. In general you should trust clients as little as possible. You can do processing on the client, but it should be related to rendering. Generally assuming that your client is malicious is a good approach to n-tier architecture applications. Especially with games.

Simple chat app (Should I use sockets? And would Apple app store approve that?)

I have an idea of an application that involves a "chat feature", basically an ability for people to chat with each other. Since sending messages through a server would be slow (plus it would be pretty bad having to check the server every second if you have new messages) I want to use sockets to have peer to peer chat ability instead of going through a server.
My 2 questions:
1) Is socket programming the most optimal way to develop a chat program? I know there is push notification service, but I don't think it can be used for a chat program too well. Going through a server seems kind of bad if you imagine 5,000 people chatting and having to poll the server every second.
2) Will Apple have issues approving an app that has peer to peer chat program that uses sockets?
Thank you.
Sockets are indeed appropriate. But you are better off with a client/server approach rather than a P2P approach.
Having worked on a very well known instant messaging service for many years, I can tell you absolutely that going through a server is not slow - as long as your server is not slow.
Client/server has lots of advantages. Namely, it's not as difficult as any sort of P2P connectivity where such issues as NATs and Firewalls make direct socket connections difficult and unreliable. Besides, you would need a messaging service anyway for clients to exchange IP addresses.
Your stated assumption that a client or server would have to "poll" is not how scalable systems work. You should use a persistent TCP socket and look at scaling a socket service up though any of the available async methods that exist today. select(), poll(), epoll on Linux, and IO Completion Ports on Windows are all techniques for having thousands of sockets simultaneously connected without periodic polling.
My suggestion - just deploy an XMPP/Jabber server. Most implementations scale up nicely to the thousands of clients. Then your chat program is just an XMPP client socket. Some of the Jabber servers even support HTTP connectivity for situations where a user's only access is via an http or http proxy server. I played around with Openfire a while back and was reasonably impresseed.
I'm fairly certain that iOS has sockets, and that it's allowed by Apple. I only know via second hand from folks that have worked on iOS products. You probably shouldn't use the push notification service for anything more than notifications to wake up your app that there is something it needs to do.
Hope this helps.

how to setup voice chat over wifi and 3G network?

I wanted to add a voice chat feature to my business app. I have been trying hard to find anything useful but there is not much help regarding this. Could anyone plz point me to something concrete??
PS: The skype app makes use of this feature.
This is a lot of work as there is no "built in" sdk features or third party off the self components that will help you do it without a lot of work on your end.
Your options (as far as I know) are:
Build your own solution.
Look for a third party solution.
Look for a open source solution.
Voice "chat" could be one of many things like:
using the voice features of a cellular network to a conference call.
using the voice features of a cellular network with a PBX server that supports conference calls.
using a VOIP solution using a SIP stack with a SIP server
using a XMPP Jingle solution (I believe the google voice service uses this)
using your own SIP setup solution
custom solution
None of these options are easy.
Open Source SIP implementations that have iphone ports (that I know of):
reSIProcate
pjsip
Update:
SIP & Jingle both use RTP for the actual transport protocol between the parties. RTP is a UDP point-to-point protocol. The ports which form a session are negotiated using other protocols such as RTSP (using SDP in the setup method) and SIP. RTP and RTCP typically use unprivileged UDP ports (1024 to 65535).
For easy Server / Client setup check Jingle Nodes in combination with SIP Communicator which is a Desktop application. But as being opensource I presume you can reuse a lot of the code to make it mobile. Specially for Android. Check this draft of a Jingle Nodes Setup Guide

Using HTTP long polling when sockets are available (e.g. iPhone, Blackberry)

I'm currently writing a simple cross platform app with Node.js on the server and web/iPhone/Blackberry clients. Bandwidth and latency requirements are similar to something you would see in an IRC "party game" or any chat system. I've developed the web client using HTTP long polling (speaking JSON both ways).
For iPhone/blackberry I could use the built in HTTP libraries to talk to my current implementation, or I could write a socket listener on the server and talk to it using sockets. Is there any advantage to doing so? Why do non-browser HTTP clients seem to be discouraged?
Can't speak to iPhone as I don't know enough about the technical details of the network stack, but for BlackBerry HTTP requests from the browser are treated differently from app-initiated requests in general. BlackBerry as a solution consists not just of a device-side TCP/HTTP stack, but the BlackBerry service, which includes (depending on if you're enterprise or not) a BlackBerry Enterprise Server with Mobile Data Services (BES/MDS) hosted on your enterprise network, or a Research In Motion hosted BlackBerry Internet Services (BIS) server, which proxy all connections from the mobile browser. These servers can do a lot of things, including handling some aspects of cookies, authentication, and content transcoding to make content more consumable by the mobile device (images and the like). For a BES/MDS they can even act as the secure endpoint in an HTTPS connection.
Anyway, this also means that a lot of the functionality you'd expect from a normal TCP/HTTP connection actually happens off the device, and so can be controlled by a carrier or enterprise or RIM. Bare-bones sockets are different because the various servers in the middle can't make as many assumptions about a TCP socket as they can about an HTTP connection, so they can't mess around with your HTTP requests. A lot of BlackBerry apps actually end up writing their own HTTP client on top of the socket layer for that very reason, so if you have to do something like an HTTP long poll (Comet?) definitely write it on top of the socket connection, not the built-in HTTP connection.

Multi-player server for iPhone application, using device as socket server

I'm working on a multiplayer iPhone application that allows up to 6 users to connect and play in "real time." I've been looking at hosted and non-hosted socket servers (SmartFox, ElectroServer, Photon/Neutron, ProjectDarkstar) and I'm wondering if anyone has any recommendations for services or implementation? Anyone have any idea of what a game like Zynga's Live Poker uses for this type of functionality or what kind of hardware you might need?
Some sub-questions:
The game is turn-based. Would it make more sense to use AMF and poll a server or should I go for the socket-based route? My current concern is concurrent connection limits and hosting costs.
Is it possible to "broadcast" a device as a socket server? i.e. once I get all my players connected, could I allocate one of the 6 devices to be a socket server and push all communication through that device? Would that be crazy? That would get around concurrency issues and I'd only need to rely on the socket server service as a lobby for the initial connection. The allocated user would stay connected to facilitate game to server communication.
1.
It's much easier to use polling, and since the game is turn based you could poll at a relatively slow rate (perhaps a couple of seconds), which means less battery drain. That said, using sockets or persistent HTTP connections would be a slicker way of doing it (and much more work). These two questions might be of interest:
How do I create a chat server that is not driven by polling?
COMET (server push to client) on iPhone
I don't know why you would use AMF. Why not JSON? Or maybe HessianKit?
2.
It makes a lot of sense to designate one of the devices as a server. Having a completely decentralized network of game clients that need to synchronize is a very hard task. Again, since your game is turn based, which doesn't require perfect real-time synchronization, you don't have to worry that having centralized state will introduce more latency.
If you intend for users to play over a local network, you should consider using GameKit.