aimed   42

Real Time Multiplayer in HTML5 - Build New Games
Real Time Multiplayer in HTML5
BocoupJul 18th, 2012
Multiplayer and browsers
When you consider making multiplayer games, there are many methods available for creating a game that friends can play online. There is a good variety of multiplayer game types - take forever example a card game you play synchronously with friends. Turns are made, information is exchanged in (semi) real time and the game progresses in discrete steps. Another example, Chess, can be asynchronous. Players take their time, contemplating possible actions and play their next move one week from now. These types of multiplayer games exist in browsers, and have for a long time. The nature of the browser itself makes it easy to make semi real time games, but we want more-visceral real time action.
Card games and Chess both usually require communication with a server and communication with the other players in order to work online. This is the foundation of a multiplayer experience to be possible - and for a long time this has existed via HTTP, where POST and GET have always been used to manage games.
The trouble with these methods is the delay, posting a message and waiting for a response each time is just too slow. It works for the semi real time and asynchronous games, but real time games require messages sent and received sometimes in the region of 33~66 times per second, something that is not quite possible with HTTP alone.
Luckily, in modern browsers we can take one step higher, and have a real time connection between a server and clients. The purpose of this discussion is to present one overview of how multiplayer games are made. We will look at input prediction, lag compensation, client interpolation and more importantly - how to do this in your normal browser using websockets. The article will present a playable demo with parameters to play with, showcasing the concepts discussed.
The technologies that we have chosen and why
Socket.io
socket.io is a powerful and flexible server-side and client-side component that enables real time networking in your browser. Not only does it support newer technologies like web sockets, but it also falls back safely onto a Flash networking layer, XHR or JSON long polling and even an HTML file transport layer. Most appealing about it perhaps is the simplicity and inherently asynchronous nature it brings, which is extremely useful when writing server and client code.
Another benefit of using socket.io is the fact that it ties into Node.js seamlessly. When coupled with Express, on connection, it can serve the client-side includes, game files, and data, making the integration clean and easy. Once you set it up, the amount of code between first connection and communication with a client is nothing short of amazing. And it would work in all browsers, mobile included.
Node.js
node.js is an easy to use, flexible and cross platform tool. It is like a swiss army knife of evented IO. It has a massive following of dedicated users, developers and module authors. It is supported on a good number of server hosting platforms for web applications, and is easy to install on a dedicated server of your own, so finding a host should not be a problem.
Among many of these great modules available for Node.js, is a web framework called Express. It covers serving files, complex routing, client authentication and sessions, and much more. It fits perfectly into the stack between socket.io and our clients, where socket.io can serve its files to the clients through Express and Express can handle the game content for us.
Canvas/HTML5
This article uses a 2D canvas to demonstrate the methods we are going to cover, which allows us to draw some text and boxes really easily.
Getting started with a real time multiplayer connection in your browser
Covering all possible options and installations and configurations for the above technology is a little out of scope for this article, but each of the above tools have their own documentation and should not take long to have set up and working together. For the sake of brevity we will dive right into making a simple game example instead. The information below is the minimum required to set up an empty canvas, connect to the socket.io server and receive messages.
Start with a simple web server, enter Express
The code for a simple Express server is really short and straightforward. It serves files on a port specified (4004 in this case) and it only serves files from the root folder (like index.html), and from a specific ‘game’ path we specify (like game/).
/* Copyright (c) 2012 Sven "FuzzYspo0N" Bergström
http://underscorediscovery.com
MIT Licensed. See LICENSE for full license.
Usage : node simplest.app.js
*/
var
gameport = process.env.PORT || 4004,
io = require('socket.io'),
express = require('express'),
UUID = require('node-uuid'),
verbose = false,
app = express.createServer();
/* Express server set up. */
//The express server handles passing our content to the browser,
//As well as routing users where they need to go. This example is bare bones
//and will serve any file the user requests from the root of your web server (where you launch the script from)
//so keep this in mind - this is not a production script but a development teaching tool.
//Tell the server to listen for incoming connections
app.listen( gameport );
//Log something so we know that it succeeded.
console.log('\t :: Express :: Listening on port ' + gameport );
//By default, we forward the / path to index.html automatically.
app.get( '/', function( req, res ){
res.sendfile( __dirname + '/simplest.html' );
});
//This handler will listen for requests on /*, any file from the root of our server.
//See expressjs documentation for more info on routing.
app.get( '/*' , function( req, res, next ) {
//This is the current file they have requested
var file = req.params[0];
//For debugging, we can track what files are requested.
if(verbose) console.log('\t :: Express :: file requested : ' + file);
//Send the requesting client the file.
res.sendfile( __dirname + '/' + file );
}); //app.get *
Socket.io, adding the real time component
Now we add the code for the socket.io part of the node.js server. It goes in the same file as the Express code, just below it as shown in the gist. The server will attach itself to Express so that it may serve the client files when requested. We do not handle any sessions in this example specifically, but you can learn about using those from this site: http://www.danielbaulig.de/socket-ioexpress/.
/* Socket.IO server set up. */
//Express and socket.io can work together to serve the socket.io client files for you.
//This way, when the client requests '/socket.io/' files, socket.io determines what the client needs.
//Create a socket.io instance using our express server
var sio = io.listen(app);
//Configure the socket.io connection settings.
//See http://socket.io/
sio.configure(function (){
sio.set('log level', 0);
sio.set('authorization', function (handshakeData, callback) {
callback(null, true); // error first callback style
});
});
//Socket.io will call this function when a client connects,
//So we can send that client a unique ID we use so we can
//maintain the list of players.
sio.sockets.on('connection', function (client) {
//Generate a new UUID, looks something like
//5b2ca132-64bd-4513-99da-90e838ca47d1
//and store this on their socket/connection
client.userid = UUID();
//tell the player they connected, giving them their id
client.emit('onconnected', { id: client.userid } );
//Useful to know when someone connects
console.log('\t socket.io:: player ' + client.userid + ' connected');
//When this client disconnects
client.on('disconnect', function () {
//Useful to know when someone disconnects
console.log('\t socket.io:: client disconnected ' + client.userid );
}); //client.on disconnect
}); //sio.sockets.on connection
Index.html, connecting a client to the server
The client side needs very little code to be able to connect to the server. As we go further, it becomes more intwined, but this is all that is required to connect to the server and send or receive data.
<!DOCTYPE html>
<html>
<head>
<title> Real time multi-player games with HTML5</title>
<style type="text/css">
html , body {
background: #212121;
color: #fff;
margin: 0;
padding: 0;
}
#canvas {
position: absolute;
left: 0; right: 0; top: 0; bottom: 0;
margin: auto;
}
</style>
<!-- Notice the URL, this is handled by socket.io on the server automatically, via express -->
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<!-- This will create a connection to socket.io, and print the user serverid that we sent from the server side. -->
<script type="text/javascript">
//This is all that needs
var socket = io.connect('/');
//Now we can listen for that event
socket.on('onconnected', function( data ) {
//Note that the data is the object we sent from the server, as is. So we can assume its id exists.
console.log( 'Connected successfully to the socket.io server. My server side ID is ' + data.id );
});
</script>
</head>
<body>
<canvas id="canvas"> </canvas>
</body>
</html>
Getting into the gameplay
What we need now is a simple interactive example for this to tie into, to get our feet wet. We are going to have two blocks running around in the same space. There is a lot of code and logic that goes into creating something that runs smoothly over the internet, and any game related code getting in the way is not as useful to this article. Instead, we focus on the multiplayer and use a simple example to demonstrate.
Some notes on developing real time games
Not all developers make games. There are some things that are new to developers that are entering the world of games that should be covered briefly.
Frame rate independence
When a block travels across the screen, it can be a simple line of code. block.position.x += 1; This 1 here, what unit is that measured in? Actually, this is one pixel - but it is moving 1 pixel per frame. Each time the loop runs, we move one pixel. That is - at 30 frames per second, it moves 30 pixels. At 60 frames per… [more]
Another  set  of  good  articles  are  from  Forrest  Smith_  who  worked  on  the  Supreme  Commander  engine  at  Gas  Powered  Games.  This  article  is  aimed  at  Real  time  Strategy  Games  (RTS):  first  part_  Synchronous  RTS  Engines  and  a  Tale  of  Desyncs  and  the  second_  Synchronous  RTS  Engines  2:  Sync  Harder.  from iphone
september 2018 by wasnotrice
G-Technology's 12-bay NAS is aimed at content creators
HGST’s external storage services brand G-Technology has announced the G-Rack 12, its first foray into the world of network attached storage. The 12-bay is aimed at small-to-medium production houses, as well as TV broadcast stations and creative teams working on video editing utilities like Adobe Premiere Pro and Final Cut Pro X. The NAS can accommodate up...

Read More

The post G-Technology’s 12-bay NAS is aimed at content creators appeared first on VR-Zone.
G-Technology’s  12-bay  NAS  is  aimed  at  content  creators 
april 2016 by vrzone
VAIO unveils new Z and S series notebooks aimed at business professionals
VAIO has announced two new additions to its notebook lineup, its second major announcement as an independent brand. The new Z and S series models are both targeted at business professionals who need long battery life and portability. The new Vaio Z flagship notebooks come in two different form-factors – flip and clamshell. The Vaio...

Read More

The post VAIO unveils new Z and S series notebooks aimed at business professionals appeared first on VR-Zone.
VAIO  unveils  new  Z  and  S  series  notebooks  aimed  at  business  professionals 
january 2016 by vrzone
Asus mulls a new sub-brand of budget motherboards aimed at the Chinese market
Asus is allegedly looking to create a new sub-brand of motherboards called Red Flag, which will be aimed at the entry-level and mid-tier segments in China. With flagging global sales, the Taiwanese vendor is looking to entice Chinese buyers with the upcoming brand. Asus sold 17 million motherboards last year, with 10 million units sold...

Read More

The post Asus mulls a new sub-brand of budget motherboards aimed at the Chinese market appeared first on VR-Zone.
Asus  mulls  a  new  sub-brand  of  budget  motherboards  aimed  at  the  Chinese  market 
january 2016 by vrzone

related tags

'mumsnet  "honest"  (rts):  -  01:11pm  02  02:00am  02:01pm  12-bay  1299  2012  2013  2014  2020  24  29  2:  3  39.500  730  @johnei:  @tagthis  a  abuse  adafruit  advanced  again’  alternate  americans  an  analyse  and/or  and  andrew  android  angelina  another  anyone  apple  applications  april  apt28  ar  arduino  are  arrows  article  articles  asus  at  at:  atmel  attack  away.  b's  bans  be  begins  being  biggest  bill  black  blackberry’s  blog  budget  builds  business  businesses_  buy  by  camera  campaign  cardi  challenge:  chinese  christians  clarkson  commander  competition  competitions  connect  consecutive  content  creators  criticized  cup  curbing  dakota’s  data  date  day  deadly  december  democrat  democratic  deployable  desantis  desyncs  developers  digital  disrupt  disrupts  drake:  drinkers'  dubbed  during  each  edition  educational  effort  elderly  election  elections  engine  engines  enthusiasts  eur  facebook  fans  favorite  features  february  first  flash  folks  for  foreigners  forrest  foul  from  front  frozen  g-technology’s  galaxy  gamers  games.  games  gas  gear  gillum  give  good  googlereader  guest  gunman  guys  hack  hacking  harder.  have  he  headset  hearing  held  hilarious  hits  hollywood's  http://linkedup-challenge.org/#sthash.byf4hrrw.dpuf  http://t.co/a5yulinz  ide  ifttt  illegal  immigration  in  industries  influence  innovative  integrate  intel  interesting  interference  internet  introduces  iranian  is  it's  itv  jack  jaws  jm  jolies  junkies  just  keyboard  kids.  kids  ladies  large-scale  launch  launched  launches  leading  linkedup  lite  live-streaming  looking  low-end  m-tech  make  mariamole  market  marketing  media  microsoft  middle-class  midterm  minaj  money  more  motherboards  mulls  music  nas  native  new  nicki  north  note  notebooks  november  nvda  of  on  online  open  opponent  out  over  packs  pages  pant  part_  platform  politics  potential  powered  prague  previous;  prize  producer  production  professionals  project  prototypes  punch  purposes.  pusha-t  racist  real  releasing  reportedly  researchers  resort  restrictions  reuters  rhetoric  ron  rookie  rs  rt  rts  ruby  running  s  said  sathi  say  screen  second_  see  seem  sends  series  set  setting  sharon's  ship  shortcuts  show  smartphone  smith_  social  sorry  special  spurs  ssd  star  storm  storms  strategy  students_  sub-brand  subscribers  supreme  sync  synchronous  systems.  tagthis  takes  tale  team  techcrunch  that  the...  the  their  there  things  think  this  three  through  thwarts  time  to  tools  top  trailers  try  tunisia  tutorial  tvi  two  unbroken  unveils  up  upon  us  users  vaio  vivo  vod  vote  voting  wanna  warning  wearable  wearablex’s  web  website  western  who  wi-fi  wilsheres  with  women  worked  working  worried  x’s  y19t  yoga  you  your  z  zealand  |      ‘entry-level’  ‘i’m  ‘smart’ 

Copy this bookmark:



description:


tags: