{"id":229,"date":"2020-09-18T18:21:58","date_gmt":"2020-09-18T18:21:58","guid":{"rendered":"http:\/\/mycours.es\/gamedesign2020\/?page_id=229"},"modified":"2020-12-11T14:00:21","modified_gmt":"2020-12-11T14:00:21","slug":"node-js-socket-io","status":"publish","type":"page","link":"http:\/\/mycours.es\/gamedesign2020\/node-js-socket-io\/","title":{"rendered":"Templates &#038; examples (node.js + socket.io)"},"content":{"rendered":"<p><a href=\"http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-230\" src=\"http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-1024x576.png\" alt=\"\" width=\"840\" height=\"473\" srcset=\"http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-1024x576.png 1024w, http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-300x169.png 300w, http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-768x432.png 768w, http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-1536x864.png 1536w, http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-2048x1152.png 2048w, http:\/\/mycours.es\/gamedesign2020\/files\/2020\/09\/1_mp91A9RzagntGGjBnwu4Yw-1200x675.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/nodejs.org\/en\/\" target=\"_blank\" rel=\"noopener noreferrer\">Download and install node.js<\/a><\/p>\n<p>We&#8217;ll look into these 3 skeleton project and introduce node, express, and socket.io:<\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/hello-node\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/molleindustria\/hello-node<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/hello-express\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/molleindustria\/hello-express<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/hello-socket\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/molleindustria\/hello-socket<\/a><\/p>\n<p><strong>p5 + socket.io (creating circles)<\/strong><\/p>\n<p>First socket.io communication between clients. Draw a circle, the circle appears in all clients:<\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/p5-socket\">https:\/\/github.com\/molleindustria\/p5-socket<\/a><\/p>\n<p>Exercises:<br \/>\n*Make the circles from different clients appear different<br \/>\n*Make a square appear on the right mouse click (or keyboard press)<br \/>\n*Network the mouse position<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Real time updates (mouse pointers)<\/strong><\/p>\n<p>Example of player management<\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/mouses\">https:\/\/github.com\/molleindustria\/mouses<\/a><\/p>\n<p><em>Exercises:<\/em><\/p>\n<p><em>*When a player clicks, the mouse pointer changes appearance (in all clients)<\/em><br \/>\n<em>*When a player clicks on another mouse pointer the clicked pointer &#8220;dies&#8221; (it should be resolved on the server side)<\/em><br \/>\n<em>*Every 5 seconds all the mouse pointers go back to life (also determined by the server)<\/em><\/p>\n<p><strong>Smoothed real time (mouses eased version)<br \/>\n<\/strong>Making movements independent from updates<strong><br \/>\n<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/mouses-smooth\">https:\/\/github.com\/molleindustria\/mouses-smooth<\/a><\/p>\n<p><em>Exercise:<\/em><\/p>\n<p>*see if you can incorporate the easing in your modded mouses<\/p>\n<p><strong>Client-side prediction (spaceship example)<br \/>\n<\/strong>Movement logic on the server side, the client smooths movements by extrapolating the positions until the next server update<\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/server-prediction\">https:\/\/github.com\/molleindustria\/server-prediction<\/a><\/p>\n<div class=\"flex-shrink-0 col-12 col-md-9 mb-4 mb-md-0\">\n<div id=\"readme\" class=\"Box md js-code-block-container Box--responsive\">\n<div class=\"Box-body px-5 pb-5\">\n<article class=\"markdown-body entry-content container-lg\"><em>Exercise:<br \/>\nExtend this logic and create a multiplayer shooting game like SpaceWar!<\/em><em>*add a state object similar to &#8220;players&#8221; that keeps track of the bullets<br \/>\n<\/em><em>*add a way to visually differentiate players or tell your ship apart<br \/>\n<\/em><em>*add a simple collision detection based on distance (circle collider)<br \/>\n<\/em><em>*add a game over \/ respawn condition<\/em><\/article>\n<\/div>\n<\/div>\n<\/div>\n<h3>Chat Example Series<\/h3>\n<p>These incremental examples illustrate some capabilities of node.js and various lobby architectures<\/p>\n<p><strong>Part 1<\/strong><br \/>\nRudimentary avatars and speech bubbles. Client side movements. Frame independent movements. p5.js <a href=\"https:\/\/p5js.org\/reference\/#group-DOM\">DOM add-on<\/a> for form and button.<\/p>\n<p><a href=\"https:\/\/glitch.com\/~molleindustria-chat1\">https:\/\/glitch.com\/~molleindustria-chat1<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/chat1\">https:\/\/github.com\/molleindustria\/chat1<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Part 2<br \/>\n<\/strong>Basic avatar creation, html UI integration<strong><br \/>\n<\/strong><\/p>\n<p><a href=\"https:\/\/glitch.com\/~molleindustria-chat2\">https:\/\/glitch.com\/~molleindustria-chat2<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/chat2\">https:\/\/github.com\/molleindustria\/chat2<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Part 3<br \/>\n<\/strong>socket.io rooms, bad word filtering<strong><br \/>\n<\/strong><strong><br \/>\n<\/strong><a href=\"https:\/\/glitch.com\/~molleindustria-chat3\">https:\/\/glitch.com\/~molleindustria-chat3<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/chat3\">https:\/\/github.com\/molleindustria\/chat3<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Part 4 (3b)<br \/>\n<\/strong>Basic lobby with players automatically assigned to rooms to cap the limit or have a games with a specific number of players.<\/p>\n<p><a href=\"https:\/\/glitch.com\/~molleindustria-chat4\">https:\/\/glitch.com\/~molleindustria-chat4<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/chat4\">https:\/\/github.com\/molleindustria\/chat4<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Advanced Server Template<\/h2>\n<p>This example is barebone on the client side &#8211; it&#8217;s a simple chat that doesn&#8217;t use p5 or any other libraries &#8211; but it includes a few technically complex features on the server side:<\/p>\n<ul>\n<li>rudimentary admin system: logging as username|password you are tagged as administrator and able to<\/li>\n<li>perform special actions (ban, mute, kick, etc)<\/li>\n<li>admin user and passwors stored in an .env private file<\/li>\n<li>admin names are reserved and the server checks for duplicate names<\/li>\n<li>players&#8217; IPs can be banned<\/li>\n<li>anti-spam system preventing too many messages from being sent by the same client<\/li>\n<li>server-side anti-flood system, if too many packets per second are detected the IP gets automatically banned (malicious attack)<\/li>\n<li>events are wrapped in try\/catch statements to prevent hacked clients from crashing the server<\/li>\n<li>all text messages and user names are filtered against a list of banned words<\/li>\n<li>inactive players are disconnected after a certain time<\/li>\n<li>system to detect when the players are away from keyboard (AFK), their tabs are inactive<\/li>\n<li>system to check that the client and server versions are aligned<\/li>\n<li>client-side quick login option that assigns a random username and automatically logs in to speed up the testing<\/li>\n<li>a basic data structure for unchangeable data (data.js on the server side)<\/li>\n<\/ul>\n<p><a href=\"https:\/\/glitch.com\/~molleindustria-server-template\">https:\/\/glitch.com\/~molleindustria-server-template<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/molleindustria\/server-template\">https:\/\/github.com\/molleindustria\/server-template<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Download and install node.js We&#8217;ll look into these 3 skeleton project and introduce node, express, and socket.io: https:\/\/github.com\/molleindustria\/hello-node https:\/\/github.com\/molleindustria\/hello-express https:\/\/github.com\/molleindustria\/hello-socket p5 + socket.io (creating circles) First socket.io communication between clients. Draw a circle, the circle appears in all clients: https:\/\/github.com\/molleindustria\/p5-socket Exercises: *Make the circles from different clients appear different *Make a square appear on the &hellip; <a href=\"http:\/\/mycours.es\/gamedesign2020\/node-js-socket-io\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Templates &#038; examples (node.js + socket.io)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-229","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/pages\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":9,"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/pages\/229\/revisions"}],"predecessor-version":[{"id":244,"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/pages\/229\/revisions\/244"}],"wp:attachment":[{"href":"http:\/\/mycours.es\/gamedesign2020\/wp-json\/wp\/v2\/media?parent=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}