Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplified socket handling

  • Loading branch information...
commit bda9f7bf86fee869e5eaa521c15ebfe12938f5ed 1 parent afaa415
Daniel authored
View
29 index.html
@@ -1,29 +0,0 @@
-<!doctype html>
-
-<html>
- <head>
- <title>RacingGame</title>
-
- <link rel="stylesheet" href="css/main.css" />
- </head>
- <body>
- <div id="canvas"></div>
- <canvas id="b2debug"></canvas>
-
- <script src="lib/three.js/build/three.js"></script>
- <script src="lib/requestAnimFrame.js"></script>
- <script src="lib/Husky.js"></script>
- <script src="lib/box2d.js/javascript/Box2D/box2d.min.js"></script>
- <script src="lib/stats.js/build/Stats.js"></script>
-
- <script src="js/RacingGame.js"></script>
- <script src="js/KeyHandler.js"></script>
- <script src="js/Camera.js"></script>
- <script src="js/Scene.js"></script>
- <script src="js/Renderer.js"></script>
- <script src="js/Car/Car.js"></script>
- <script src="js/Car/UserCar.js"></script>
- <script src="js/Wall.js"></script>
- <script src="js/main.js"></script>
- </body>
-</html>
View
18 public/js/RacingGame.js
@@ -2,6 +2,8 @@ var RacingGame = (function() {
function RacingGame(container, b2debugCanvas) {
this.model = {};
+ this.socket = this.setupSocket('http://localhost/');
+
this.shouldDebug = false;
this.b2DebugDraw = this.setupDebugDraw(b2debugCanvas);
@@ -11,8 +13,6 @@ var RacingGame = (function() {
this.keyHandler = new KeyHandler();
- this.socketHandler = new SocketHandler(this.model);
-
this.model.camera = new Camera(50, window.innerWidth * .5 / window.innerHeight, 0.001, 1000);
this.model.scene = new Scene();
this.model.renderer = new Renderer(container, this.model.scene, this.model.camera);
@@ -22,13 +22,25 @@ var RacingGame = (function() {
this.model.userCar = new UserCar(this.model.scene, this.model.b2World, this.keyHandler);
this.model.cars.push(this.model.userCar);
- for(var i = 0; i < 5; i++) {
+ for(var i = 0; i < 10; i++) {
this.model.cars.push(new Car(this.model.scene, this.model.b2World, Math.random() * 10, Math.random() * 10, Math.random() * Math.PI));
}
this.model.wall = new Wall(this.model.scene, this.model.b2World, 3, 3, 10, 1.2, 1, Math.random()*Math.PI);
}
+ RacingGame.prototype.setupSocket = function(url) {
+ var socket = io.connect(url);
+
+ socket.on('connecting', function(message) { console.log('Connecting to ', url) });
+ socket.on('connect_failed', function(message) { console.log('Connection failed') });
+ socket.on('connect', function(message) { console.log('Connected to socket!') });
+ socket.on('disconnect', function(message) { console.log('Disconnected from socket!') });
+ socket.on('message', function(message) { console.log('Socket message:', message) });
+
+ return socket;
+ }
+
RacingGame.prototype.setupDebugDraw = function(debugCanvas) {
var game = this;
debugCanvas.addEventListener('click', function() {
View
55 public/js/SocketHandler.js
@@ -1,55 +0,0 @@
-var SocketHandler = (function() {
- var socketHandler;
-
- function SocketHandler(model) {
- socketHandler = this;
-
- this.userId = null;
- this.model = model;
-
- this.hasConnection = false;
-
- this.socket = io.connect('http://localhost');
-
- this.socket.on('connect', this.connect);
- this.socket.on('disconnect', this.disconnect);
- this.socket.on('message', this.processMessage);
-
- this.socket.on('connecting', function(e) {
- console.log('connecting...', e);
- });
-
- this.socket.on('connect_failed', function(e) {
- console.log('connection failed', e);
- });
- }
-
- SocketHandler.prototype.welcomeHandler = function(data) {
- this.userId = data.id;
- console.log('server welcomed me! I have an ID now: ', this.userId);
- }
-
- SocketHandler.prototype.connect = function() {
- console.log('Socket opened!');
- this.hasConnection = true;
- }
-
- SocketHandler.prototype.disconnect = function() {
- console.log('Socket closed!');
- this.hasConnection = false;
- }
-
- SocketHandler.prototype.send = function(type, data) {
- if(!data) { data = {} }
- data['type'] = type;
- this.socket.send(data);
- }
-
- SocketHandler.prototype.processMessage = function(data) {
- if(socketHandler[data.type + 'Handler']) {
- socketHandler[data.type + 'Handler'](data);
- }
- }
-
- return SocketHandler;
-})();
View
10 server.js
@@ -15,8 +15,7 @@ app.configure(function(){
app.get('/', function(req, res){
res.render('index', {
- layout: false,
- title: 'Express'
+ layout: false
});
});
@@ -25,12 +24,7 @@ app.listen(3000);
io = io.listen(app);
io.sockets.on('connection', function (client) {
-
- console.log('User connected on socket');
-
- client.json.send({
- 'type': 'welcome'
- });
+ client.send('Welcome to the server!');
});
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
1  views/index.ejs
@@ -27,7 +27,6 @@
<script src="js/Car/Car.js"></script>
<script src="js/Car/UserCar.js"></script>
<script src="js/Wall.js"></script>
- <script src="js/SocketHandler.js"></script>
<script src="js/main.js"></script>
</body>
Please sign in to comment.
Something went wrong with that request. Please try again.