Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js windows中具有localhost的nodejs中的socket.io.js路径_Node.js_Sockets - Fatal编程技术网

Node.js windows中具有localhost的nodejs中的socket.io.js路径

Node.js windows中具有localhost的nodejs中的socket.io.js路径,node.js,sockets,Node.js,Sockets,我已经安装了node.js v0.10.31和npm v1.4.3,还安装了node uuid、socket.io和express 我正在尝试创建一个简单的聊天应用程序,但是我提到的socket.io路径非常混乱。我安装的socket.io路径是D:\program files\nodejs\node\u modules\socket.ioxampp中本地主机中的我的项目文件(对于php) 参考教程链接/socket.io/socket.io.js,但我的链接位于 D:\program file

我已经安装了node.js v0.10.31和npm v1.4.3,还安装了node uuid、socket.io和express

我正在尝试创建一个简单的聊天应用程序,但是我提到的socket.io路径非常混乱。我安装的socket.io路径是D:\program files\nodejs\node\u modules\socket.ioxampp中本地主机中的我的项目文件(对于php)

参考教程链接/socket.io/socket.io.js,但我的链接位于 D:\program files\nodejs\node\u modules\socket.io\node\u modules\socket.io client\socket.io.js

如何使用将
包含到

我的项目中有两个文件,我将node_modules文件夹从nodejs复制到项目文件夹中

app.js

var常量={court:{width:600,height:600},
桨叶:{宽度:50,高度:15,三角形:3},
球:{半径:10,deltaLeft:3,deltaTop:2,间隔:30}
};                         
var状态={桨:{},
球:{左:0,上:0},
底部桨:0,
倾倒地址:0,
左桨:0,
右桨:0
};
var serverState={intervalId:0,
连接:0
};
var app=require('http')。createServer(处理程序),
io=require('socket.io')。侦听(应用程序),
fs=要求('fs');
app.listen(80);
函数处理程序(req、res){
fs.readFile(uu dirname+'/index.html',,
函数readfile_回调(错误,数据){
如果(错误){
书面记录(500);
返回res.end('Error loading index.html');
}
书面记录(200);
res.end(数据);
}
);
};
函数calculateBallPosition(){
var left=state.ball.left+constants.ball.deltaLeft;
var top=state.ball.top+constants.ball.deltaTop;
如果(左>=常数.court.width){
左=常数.court.width;
constants.ball.deltaLeft=-constants.ball.deltaLeft;
}else if(左=常数.COUNT.height-常数.BAILD.height){
如果(状态。底部挡板&&
左>((state.palles[state.bottompalle]/100)*constants.court.width-constants.palle.width/2)&&
(左<((state.pailes[state.bottompaile]/100)*constants.court.width+constants.paile.width/2))){
top=constants.court.height-constants.blade.height-constants.ball.radius;
constants.ball.deltaTop=-constants.ball.deltaTop;
}否则{
//待办事项:#1
左=常数.court.width/2;
top=常数.court.height/2;
}
}否则,如果(顶部=100)
newLeft=100;

否则如果(newLeft如果您在http服务器上正确设置了socket.io(您没有向我们显示代码的那一部分),那么它会自动处理对
“/socket.io/socket.io.js”的请求
并将
socket.io.js
文件提供给实际驻留在其他地方的客户端。换句话说,它通过拦截
“/socket.io/socket.io.js”的http请求来为您处理所有这些
并从socket.io服务器代码来自的node_modules目录中的位置提供所需的文件。

我使用npm install socket.io进行安装。如何使用http服务器进行安装?向我们展示您用于初始化web服务器和socket.io的代码。正是这些代码使其工作。我不确定,但您需要修改lso需要在项目中本地而不是全局安装socket.io。请转到项目目录,然后键入
npm install socket.io
。我在项目文件夹“node\u modules”中安装了socket.io…我添加了有问题的代码,错误未捕获引用错误:未定义io,找不到socket.io文件…谢谢advance@Dhamu-您在服务器控制台和客户端控制台中看到了哪些错误?我看不出代码有任何明显的错误,因此您必须进行故障排除,以收集更多信息以获得线索。错误未捕获参考错误:未定义io,未在浏览器控制台中找到socket.io文件…的可能副本
var constants = { court: { width: 600, height: 600 }, 
                  paddle: { width: 50, height: 15, delta: 3 },
                  ball: { radius: 10, deltaLeft: 3, deltaTop: 2, interval: 30 }
                };                         

var state = { paddles: {},
              ball: { left: 0, top: 0 },
              bottomPaddle: 0,
              topPaddle: 0,
              leftPaddle: 0,
              rightPaddle: 0
            };

var serverState = { intervalId: 0, 
                    connections: 0
                  };

var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    fs = require('fs');

app.listen(80);

function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
        function readfile_callback(err, data) {
            if (err) {
                res.writeHead(500);
                return res.end('Error loading index.html');
            }
            res.writeHead(200);
            res.end(data);
        }
    );
};

function calculateBallPosition() {
    var left = state.ball.left + constants.ball.deltaLeft;
    var top = state.ball.top + constants.ball.deltaTop;

    if (left >= constants.court.width) {
        left = constants.court.width;
        constants.ball.deltaLeft = -constants.ball.deltaLeft;
    } else if (left <= 0) {
        left = 0;
        constants.ball.deltaLeft = -constants.ball.deltaLeft;
    }
    if (top + constants.ball.radius >= constants.court.height - constants.paddle.height) {
        if (state.bottomPaddle && 
            left > ( (state.paddles[state.bottomPaddle]/100) * constants.court.width - constants.paddle.width / 2) &&
            (left < ( (state.paddles[state.bottomPaddle]/100) * constants.court.width + constants.paddle.width / 2) ) ) {
            top = constants.court.height - constants.paddle.height - constants.ball.radius;
            constants.ball.deltaTop = -constants.ball.deltaTop;
        } else {
            //TODO: #1
            left = constants.court.width / 2;
            top = constants.court.height / 2;       
        }
    } else if (top <= 0) {
        top = 0;
        constants.ball.deltaTop = -constants.ball.deltaTop;
    }
    state.ball.left = left;
    state.ball.top = top;
};

io.sockets.on('connection', function (socket) {

    var paddleAdded = false;
    if (!state.bottomPaddle) {
        state.bottomPaddle = socket.id;
    } else if (!state.topPaddle) {
        state.topPaddle = socket.id;
    } else if (!state.leftPaddle) {
        state.leftPaddle = socket.id;
    } else if (!state.rightPaddle) {
        state.rightPaddle = socket.id;
    } else {
      // placeholder for fifth player
      return;
    }

    state.paddles[socket.id] = 50;

    socket.emit('environment', { court:  {  width:  constants.court.width, 
                                            height: constants.court.height,
                                         }, 
                                 paddle: {  width: constants.paddle.width, 
                                            height: constants.paddle.height,
                                            delta: constants.paddle.delta
                                         },
                                 ball: { radius: constants.ball.radius },
                                 player: { id: socket.id }
    });

    if ( !serverState.intervalId ) {
        serverState.intervalId = setInterval( function(){
            calculateBallPosition();
        }, constants.ball.interval );  
    }

    socket.intervalId = setInterval( function(){
        socket.emit('ball', { position: { left: state.ball.left, top: state.ball.top } }); 
        socket.emit('paddles', { positions: state.paddles, sides: {bottom: state.bottomPaddle, top: state.topPaddle, left: state.leftPaddle, right: state.rightPaddle }});
    }, constants.ball.interval );  

    socket.on('paddle', function (data) {
        state.paddles[socket.id] = data.left;
    });

    socket.on('disconnect', function () {
        serverState.connections--;
        clearInterval( socket.intervalId );
        delete state.paddles[socket.id];

        if (state.bottomPaddle == socket.id)
            state.bottomPaddle = 0;
        else if (state.topPaddle == socket.id)
            state.topPaddle = 0;
        else if (state.leftPaddle == socket.id)
            state.leftPaddle = 0;
        else if (state.rightPaddle == socket.id)
            state.rightPaddle = 0;
        if ( serverState.connections == 0 ) {
            clearInterval( serverState.intervalId );
            serverState.intervalId = 0;
        }
        console.log('player left');
    });  

    console.log(serverState.connections);
    serverState.connections++;
});
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <style>
      .topPlayer {
        -moz-transform: rotate(180deg);
      }
      .leftPlayer {
        -moz-transform: rotate(270deg);
      }
      .rightPlayer {
        -moz-transform: rotate(90deg);
      }
    </style>
  </head>
  <body>
    <canvas id="court"></canvas>

    <script src="/socket.io/socket.io.js"></script>
    <script>

  var constants = { court: { width: 0, height: 0, adjusted: false }, 
                    colors: { court: "brown", ball: "black", paddle: "orange" },
                    paddle: { width: 0, height: 0, delta: 0 },
                    ball: { radius: 0 },
                    player: { id: 0 }
                  };                               

  var state = { paddles: {},
                ball: { left: 0, top: 0 },
                sides: {}
              };

  var socket = io.connect('http://localhost/pingv1'),
      canvas = document.getElementById("court"),
      ctx = canvas.getContext('2d');

  socket.on('environment', function(data) {
    constants.court.width = data.court.width;
    constants.court.height = data.court.height;
    constants.paddle.delta = data.paddle.delta;
    constants.paddle.width  = data.paddle.width;
    constants.paddle.height  = data.paddle.height;
    constants.ball.radius = data.ball.radius;   
    constants.player.id = data.player.id;    
  });

  socket.on('paddles', function(data) {
    var paddles = data.positions;
    // Overwrite the server's version of my own paddle position
    // if I already know where I am so I don't redraw in the old spot.
    if (state.paddles[constants.player.id])
      paddles[constants.player.id] = state.paddles[constants.player.id];
    state.paddles = paddles;
    state.sides = data.sides;
    if (!constants.court.adjusted) {
        constants.court.adjusted = true;
        if (state.sides.top == constants.player.id)
            canvas.className = 'topPlayer';
        else if (state.sides.left == constants.player.id)
            canvas.className = 'leftPlayer';
        else if (state.sides.right == constants.player.id)
            canvas.className = 'rightPlayer';
    }
  });

  socket.on('ball', function (data) {
    state.ball.left = data.position.left;
    state.ball.top = data.position.top;
    drawCanvas();     
  });

  var drawCanvas = function() {
    canvas.width = constants.court.width;
    canvas.height = constants.court.height;
    ctx.fillStyle = constants.colors.court;
    ctx.fillRect(0, 0, constants.court.width, constants.court.height);
    ctx.fillStyle = constants.colors.paddle;
    ctx.fillRect((state.paddles[state.sides.bottom] / 100 * constants.court.width) - (constants.paddle.width / 2),
                 constants.court.height - constants.paddle.height, constants.paddle.width, constants.paddle.height);
    ctx.fillRect((state.paddles[state.sides.top] / 100 * constants.court.width) - (constants.paddle.width / 2),
                 0, constants.paddle.width, constants.paddle.height);
    ctx.fillRect(0, (state.paddles[state.sides.left] / 100 * constants.court.height) - (constants.paddle.height / 2),
                 constants.paddle.height, constants.paddle.width);
    ctx.fillRect(constants.court.width - constants.paddle.height,
                 (state.paddles[state.sides.right] / 100 * constants.court.height) - (constants.paddle.height / 2),
                 constants.paddle.height, constants.paddle.width);
    ctx.fillStyle = constants.colors.ball;
    ctx.beginPath(); 
    ctx.arc( state.ball.left, state.ball.top, constants.ball.radius, 0, Math.PI * 2 );
    ctx.fill();
  };

  var movePaddle = function (delta) {
    var newLeft = state.paddles[constants.player.id] + delta;
    if (newLeft >= 100)
      newLeft = 100;
    else if (newLeft <= 0)
      newLeft = 0;
    if (newLeft != state.paddles[constants.player.id]) {
      state.paddles[constants.player.id] = newLeft;
      socket.emit('paddle', {left: state.paddles[constants.player.id] });
      drawCanvas();
    }
  };

  window.addEventListener('keydown', function onKeyDown(aEvent) {
    switch (aEvent.which) {
      case 37: // Left
        if (state.sides.top == constants.player.id || state.sides.right == constants.player.id) movePaddle(constants.paddle.delta);
        else movePaddle(-constants.paddle.delta);
        break;
      case 39: // Right
        if (state.sides.top == constants.player.id || state.sides.right == constants.player.id) movePaddle(-constants.paddle.delta);
        else movePaddle(constants.paddle.delta);
        break;
    }
  }, false);

    </script>  
  </body>
</html>