Browse Source

Set up socket.io connection

master
jrtechs 6 years ago
parent
commit
34bb248a95
3 changed files with 47 additions and 15 deletions
  1. +8
    -5
      front end/index.html
  2. +1
    -4
      front end/singlePlayer.html
  3. +38
    -6
      server/server.js

+ 8
- 5
front end/index.html View File

@ -36,12 +36,14 @@
var players = {}; var players = {};
var bullets = {};
var blocks = {};
socket.on("connected", function(data) socket.on("connected", function(data)
{ {
players = data.player;
bullets = data.bullet;
console.log("got message connected");
console.log(data);
players = data.players;
blocks = data.blocks;
}); });
socket.on("update",function(data) socket.on("update",function(data)
@ -56,6 +58,7 @@
function doKeyDown(e) function doKeyDown(e)
{ {
console.log("key down");
socket.emit("keyUp",{"direction":e.keyCode}); socket.emit("keyUp",{"direction":e.keyCode});
} }
@ -64,12 +67,12 @@
context.fillStyle = "#000000"; context.fillStyle = "#000000";
context.fillRect(0, 0, width, height); context.fillRect(0, 0, width, height);
for(var i = 0; i< bullets.length; i++)
for(var i = 0; i< blocks.length; i++)
{ {
if(bullets[i] != -1) if(bullets[i] != -1)
{ {
context.fillStyle = "rgba(255, 0, 199, 1)"; //pink context.fillStyle = "rgba(255, 0, 199, 1)"; //pink
context.fillRect(bullets[i].x, bullets[i].y, 10,10);
context.fillRect(blocks[i].x, blocks[i].y, 10,10);
} }
} }

+ 1
- 4
front end/singlePlayer.html View File

@ -1,5 +1,4 @@
<script src="/socket.io/socket.io.js"></script>
<script src="../server/server.js"></script>
<script> <script>
@ -382,8 +381,6 @@
} }
var socket = io();
</script> </script>

+ 38
- 6
server/server.js View File

@ -5,19 +5,51 @@
* 2-22-18 * 2-22-18
*/ */
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const PORT = 3000;
var PORT = 3000;
var players
var players = {};
io.on('connection', function(socket)
var blocks = [];
var Player = function()
{ {
}; };
io.on('connection', function(socket)
{
var p = new Player();
console.log("user connected");
var initialState = new Object();
initialState.players = players;
initialState.blocks = blocks;
socket.emit('connected', initialState);
socket.on('move', function(data)
{
});
socket.on('shoot', function(data)
{
});
socket.on('disconnect', function()
{
console.log("user disconnected");
})
});
http.listen(PORT, function() http.listen(PORT, function()
{ {
console.log('listening on *:3000'); console.log('listening on *:3000');

Loading…
Cancel
Save