This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//We will call this file server.js | |
var app = require('express').createServer(), | |
io = require('socket.io').listen(app), | |
url = require('url'), | |
pth = require('path'), | |
UUID = require('node-uuid'); | |
/* Start of express/web server code. note this is just thrown here quick so you can grasp how it works. This obviously isn't good code or anything. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Game = Class.extend({ | |
init : function( player, opponent ) { | |
this.id = UUID(); | |
this.player = player; | |
this.opponent = opponent; | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Server = Class.extend({ | |
init : function( ) { | |
this.connections = []; | |
this.games = []; | |
}, | |
addConnection : function( connection ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Session = Class.extend({ | |
init : function( socket ) { | |
this.socket = socket; | |
this.serverid = UUID(); | |
this.initListeners(); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
io.sockets.on('connection', function (socket) { | |
//create a new player, which registers its events | |
var connection = new Session( socket ); | |
//add to the list of connections (for finding a game) | |
server.addConnection( connection ); | |
//This will send a message to all clients saying that they connected, | |
//And hand them their serverid for future interactions. | |
socket.emit('onConnect', { serverid : connection.serverid }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Ball = Class.extend({ | |
init : function( attachTo, isServer ) { | |
//Only create meshes on the client side | |
if( !isServer ) { | |
//Create meshes | |
this.setupBall( ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Ball = Class.extend({ | |
init : function( attachTo ) { | |
//Create meshes | |
this.setupBall(); | |
//Store the speed value | |
this.speed = 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Player = Class.extend({ | |
init : function( remoteConnection ) { | |
//The opponent we are storing is actually the socket for the player connection to socket.io | |
this.connection = remoteConnection || false; | |
//Create the mesh and set the positions for the player on screen | |
this.setupPaddle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> Real time multi-player games with HTML5</title> | |
<style type="text/css"> | |
html , body { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create and listen for clients on the existing web server | |
//we created above. app is the express server. | |
var io = require('socket.io').listen( app ); | |
//We also introduce a UUID library, for unique identifiers. | |
//Not required, but I find useful. | |
var UUID = require('node-uuid'); | |
/* Socket.io Configuration */ |
OlderNewer