GithubHelp home page GithubHelp logo

ruby0x1 / realtime-multiplayer-in-html5 Goto Github PK

View Code? Open in Web Editor NEW
903.0 903.0 271.0 55 KB

Read the full article: http://buildnewgames.com/real-time-multiplayer/ An example using node.js, socket.io and HTML5 Canvas to explain and demonstrate realtime multiplayer games in the browser.

License: MIT License

JavaScript 98.29% CSS 0.40% HTML 1.31%

realtime-multiplayer-in-html5's People

Contributors

dethe avatar ruby0x1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

realtime-multiplayer-in-html5's Issues

Different Clients sending packets faster than others - resulting in faster client speed

First time I've posted an issue in github, so I'm probably doing it all wrong. I've noticed that running the client on my Windows machine, is always resulting in a client that moves faster than on my Mac. Both devices have similar lag (between 1 and 5ms) however my Windows machine gets around double the FPS than the Mac. I dont think FPS is the issue as you dont calculate FPS in any physics code.

So I went looking and the only thing I can really tell at this stage is that the amount of input (and thus input rate) coming from the Windows PC is about double (402 vs 258) that of the Mac client for a given set time. My counter was in game_core.prototype.process_input = function( player ) and was measuring the frequency of each individual client update.

And because the code doesn't have input rate limiting, those clients effectively end up with a faster moving client, as its all based on input rate to trigger the physics world updates in the Server loop.

I suspect this is less of a problem over real life scenarios that have at least a lag of 10-50ms, but even for a simple LAN game, there is unfair advantage here in the code.

I have ideas how to fix this, but I'm going to struggle anytime soon.

I'd also like to support a variable amount of clients (say max 32) per game, so I'd like to attempt to author the code to support this. But again, javascript is very new to me :(

sending client input state only on frames where there is a keydown would not work when gravity is present

hello!

this repo is really great. :) I've been using it to learn more about networking in a very hands on way.

One thing that I discovered is the current implementation only sends client input state to the server when there is at least one of the keys down. This is totally fine for a simulation/game where a unit can only move due to direct player input, but if one were making a 2d side scrolling platformer like mario where the character is affected by gravity and other forces every frame, the physics step won't happen on those frames where input is missing.

There's a simple fix: always send the input messages to the server, even if the input array is empty. Basically just eliminate this if statement, and the entire else clause: https://github.com/ruby0x1/realtime-multiplayer-in-html5/blob/main/game.core.js#L520

thanks for a really great article and repo!

TypeError: Arguments to path.join must be strings

node app was run successfully, but every time I access http://127.0.0.1:4004/?debug error is showing up. Here the error message

โžœ realtime-multiplayer-in-html5 git:(master) โœ— node app
:: Express :: Listening on port 4004
info - socket.io started
TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at exports.join (path.js:358:36)
at exports.send (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/node_modules/connect/lib/middleware/static.js:129:20)
at ServerResponse.res.sendfile (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/response.js:186:3)
at app.get.file (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/app.js:36:13)
at callbacks (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:272:11)
at param (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:246:11)
at pass (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:280:5)
TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at exports.join (path.js:358:36)
at exports.send (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/node_modules/connect/lib/middleware/static.js:129:20)
at ServerResponse.res.sendfile (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/response.js:186:3)
at /Users/novalagung/Downloads/realtime-multiplayer-in-html5/app.js:52:13
at callbacks (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:272:11)
at param (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:246:11)
at pass (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/Users/novalagung/Downloads/realtime-multiplayer-in-html5/node_modules/express/lib/router/index.js:280:5)

Serving socket.io.js

Wonder if you know if there are any issue with socket.io and node.js 0.8.2. Im getting a bunch of these and nothing in Chromes developer toolbar.

Error: ENOENT, stat '/Users/christian/Development/realtime-multiplayer-in-html5/socket.io/socket.io.js'

package.json

Why not add a package.json so instead of readme instructions like "Install socket.io npm install socket.io" you can just do "npm install" in the clone directory?

would you accept a PR to convert to es modules + data oriented design?

I have a working port of this code which basically does 3 things:

  • switches the code from object oriented syntax to data oriented (all functions in game-core.js accept a game-core object.)
  • in server side, replace requestAnimationFrame with setTimeout (removes need for polyfill)
  • switches to es modules for both the client and server

benefits:

  • no more this peppered all over the code
  • no more need to put references to window or global into the code that runs across browser

I realize this is a super old project, and it's oriented towards learning, but maybe this change would be welcome? If so, happy to send a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.