Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var app = express();
- var http = require('http').createServer(app);
- var io = require('socket.io')(http);
- var users = [];
- app.use("/", express.static(__dirname+"/client"));
- io.on('connection', function(socket){
- console.log('a user connected');
- socket.on("adduser", function(data){
- var user_id = users.length+1;
- socket.fakeid = user_id;
- //socket.users
- users[user_id] = {
- id: user_id,
- soc: socket.id,
- name: data,
- x: 0,
- y: 0
- }
- console.log(data + " Join game");
- console.log(users);
- socket.emit("update_users", users);
- });
- socket.on("start_move", function(data){
- console.log(users);
- if(data == "right"){
- users[socket.fakeid].x += 10;
- }
- if(data == "down"){
- users[socket.fakeid].y += 10;
- }
- if(data == "left"){
- users[socket.fakeid].x -= 10;
- }
- if(data == "up"){
- users[socket.fakeid].y -= 10;
- }
- socket.emit("update_users", users);
- });
- socket.on('disconnect', function(){
- console.log('a user disconnected');
- });
- });
- http.listen(80, function(){
- console.log('listening on *:80');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement