I have built a simple node.js 2-page app. A lobby, and a game room. The user connects to the lobby, then takes a seat at a table and the game room opens up in a new window. So two browser windows open, both with their own socket connection.
Everything works great on my computers. Works on all browsers, and the user stays connected in the lobby while they are playing the game in the game room as i'm ping-ponging a signal back-and-forth.
However, on my iPad or iPhone, when I open up the game room, the socket connection to the lobby ends after a short amount of time.
Is there a best practice for forcing a socket connection to stay open on those devices that's different than a computer?
Here's the code I have on the server:
EDIT: The connection to the lobby actually ends instantly when that browser window no longer has focus on a mobile device, it's just that the timeout itself happens after the timeout interval.
Here's my debug output:
got pong Player1
got pong Player1
got pong Player1
got pong Player1
got pong Player1
{ username: 'Player2',
password: 'password',
browser_os: 'Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3' }
info - handshake authorized Ut2WHuhbDGsYzd6CRquX (player 2 logs in and handshakes the lobby)
connecting to lobby
got pong Player1
got pong Player2 (first pong from player 2)
info - handshake authorized If6MFTkJqv3T_Mn5RquY (game table opens for player 2 - notice no more pongs from Player 2!!)
got pong Player1
got pong Player1
got pong Player1
info - handshake authorized agmZNNSnce0h68NSRquZ (game table opens for player 1)
got pong Player1
got pong Player1
got pong Player1
got pong Player1
got pong Player1
got pong Player1
got pong Player1
info - transport end (heartbeat timeout)
lobby disconnect (player 2 gets disconnected from the lobby)
got pong Player1
This is my server-side code:
function sendHeartbeat(){
setTimeout(sendHeartbeat, 8000);
io.sockets.in('lobby1').emit('ping', { beat : '1' });
}
setTimeout(sendHeartbeat, 8000);
This is my client-side code:
socket.on('ping',function(msg){sendPong(msg)});
function sendPong(msg) {
socket.emit('pong');
}
Techniques and technologies for doing this are generally referred to under the umbrella term Commet, which basically comes down to forcing the browser to keep requests open for relatively long periods of time, waiting for updates from the server.
HTML5 supports a feature called Websockets that allow for a more true TCP/IP socket that creates a persistent two way connection between the browser and server. This might be your best bet as most Commet techniques are basically hacks to get around the nature of how HTTP and web browsers were originally intended to work (that is, to make small, discrete requests rather than keeping connections open indefinitely). HTML5 web sockets are intended to directly support this feature. Most modern browsers support web sockets, including mobile ones (except Android but I gotta thinks that's coming soon). There are a couple of web socket packages for node.js including WebSocket-Node, and ws. Here is a good intro into web sockets.
Why your code is working in some environments and not others is hard to say at this point. It could be lack of support in the browsers. Mobile devices tend to have less powerful browsers than desktops. I'm not sure about the support for Commet or HTML5 web sockets in mobile Safari (have a look at this).
Keep in mind that any of these techniques create overhead on both the client and the server in order to keep that connection open. This also has impacts on scalability since certain clients are bound to certain servers.
Related
I create a 2D ffa multiplayer game for android with Unity/Netcode. Now my question is how i could set up a dedicated Server with matchmaking. Do i have one Unity instance who is waiting for requests. And when it received enaugh question. It creates a new Unity instance as 'real' server and send pack the port. But we only have around 64k ports. So i would realy intrested how at the end, the server is started. Or is there only one Unity instance with multiple game Scences? I know unity has a hosting/matchmaking service. But i have not much money. And i want run specific code on Server for other stuff in the game for example database and a Trading system. Maybe somebody has an idea.
Thanks for your time.Lyksel
I was thinking of openmatch but i didn't find out if i can use it on a dedicated server.
I developing a fps game with Unet LLAPI and HLAPI network solutions(LLAPI is for lobby server and HLAPI is for game server). When I test servers in localhost, everything is ok but when I test game servers in a dedicated server, the all clients are disconnecting from server unexpectedly. It happens randomly and it does not always happen. When the all clients are disconnect, servers(game server and lobby server) still lives. This problem is happening both on the game server(hlapi) and on the lobby server(llapi). The clients are receive "disconnected by timeout" log after disconnected. How to fix this issue?
With the information provided the only answer that I can elaborate is:
Try to set the disconection time out to a higher value, sometimes the "micro-disconections" on the clients are readed on server as "full-disconections" if the Time out is set to a lower value.
Also check your Unity version, as this post suggest: UNet randomly disconnects
Try a higher port number on Ubuntu. 17000 works but 1700 does not.
I am building a multiplayer, turn based cards game. Since the answer to these question is not available in the docs, I am asking it here:
The game uses Unity's networking HLAPI (NetworkClient and NetworkServer), and the client & server communicate exclusively with NetworkMessages, no RPC is used. What happens when:
(a) The client on an android smartphone is connected to the server & playing, and the player suddenly receives a call? With the client disconnect, or will it continue to send messages, or will it stall, not accepting any messages?
(b) The player is playing over 3G and suddenly decides to switch over to Wi-Fi with the game still running. Will the client disconnect, or will the game somehow continue to run and the client stays connected to the server?
a) If you are using 3g & receive call then , its obvious player will get disconnected .
b) If you are on wifi and receive call then , you will not get disconnected if your android can handle keeping your game in memory .
c) If you switch network , the disconnection will depend upon game server .
Server will handle the part if the player reconnection should be whole new or just reconnect were he left .
Looking to your game , the server should reconnect as new connection .
i'm making a multiplayer game with unity.
i'm using master server .player1(server) will be server and player2(client) will connect to player1.
now i need to understand which player disconnected?
player1 can use OnPlayerDisconnected(NetworkPlayer) to check if player2 disconnected or not .but can't use this function in client side (only work in server side)
what player2 can do to understand player1 is disconnected or not ?
Network broadcasts another message, OnDisconnectedFromServer. It is called on the client when it is disconnected from the server.
Network.OnDisconnectedFromServer
It is passed a NetworkDisconnect object that gives you information about the disconnect event, such as whether it was a lost connection or an intentional disconnect. The Unity Script Reference page has examples for that as well.
I'm working on my multiplayer game for wp7.
At this moment i successfully implemented the multiplayer game on Udp on UdpMultiCastClient.
Well, without the phone itself i cannot test it, but from other various site(SO included) it seems UDP packet won't cross over outside of the router. So I wanted to implement a TCP P2P, so each game client connects to everyone else, so data wont be proxied through server.
But the socket class on WP7 doesn't have Listen method, neither AcceptAsync.
Is it normal?
It unfortunately seems like you can't bind a WP7.5 socket to listen for incoming connection on a specific port. So the phone can't act as a server. This is really a shame. TCP P2P connections would be awesome.
Hopefully this will be implemented into next version of Windows Phone. Meanwhile the easiest solution is probably to use a server in the middle which both devices connect to.
Edit:
Socket Listeners is available in WP8.1 ...
https://msdn.microsoft.com/en-us/library/windows/apps/hh202858(v=vs.105).aspx
https://msdn.microsoft.com/en-us/library/windows/apps/hh202874(v=vs.105).aspx