Advertisement
myarkqub

game01 - app.js

Aug 22nd, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var app = express();
  3. var http = require('http').createServer(app);
  4. var io = require('socket.io')(http);
  5.  
  6. var users = [];
  7.  
  8. app.use("/", express.static(__dirname+"/client"));
  9.  
  10. io.on('connection', function(socket){
  11.     console.log('a user connected');
  12.  
  13.     socket.on("adduser", function(data){
  14.         var user_id = users.length+1;
  15.         socket.fakeid = user_id;
  16.         //socket.users
  17.         users[user_id] = {
  18.             id: user_id,
  19.             soc: socket.id,
  20.             name: data,
  21.             x: 0,
  22.             y: 0
  23.         }
  24.         console.log(data + " Join game");
  25.         console.log(users);
  26.         socket.emit("update_users", users);
  27.     });
  28.  
  29.     socket.on("start_move", function(data){
  30.         console.log(users);
  31.         if(data == "right"){
  32.             users[socket.fakeid].x += 10;
  33.         }
  34.         if(data == "down"){
  35.             users[socket.fakeid].y += 10;
  36.         }
  37.         if(data == "left"){
  38.             users[socket.fakeid].x -= 10;
  39.         }
  40.         if(data == "up"){
  41.             users[socket.fakeid].y -= 10;
  42.         }
  43.         socket.emit("update_users", users);
  44.     });
  45.  
  46.     socket.on('disconnect', function(){
  47.         console.log('a user disconnected');
  48.       });
  49.   });
  50.  
  51. http.listen(80, function(){
  52.   console.log('listening on *:80');
  53. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement