0

我找到了一个教程,帮助我设置了一个基本的实时协作绘图页面。它在 localhost:8080 上完美运行,但我想在我的服务器上启动它,但遇到了一些问题。

应用程序.js

    // Including libraries

    var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    static = require('node-static'); // for serving files

    // This will make all the files in the current folder
    // accessible from the web
    var fileServer = new static.Server('./');

    // This is the port for our web server.
    // you will need to go to http://localhost:8080 to see it
    app.listen(8080);

    // If the URL of the socket server is opened in a browser
    function handler (request, response) {

      request.addListener('end', function () {
        fileServer.serve(request, response); // this will return the correct file
      });
    }

    // Delete this row if you want to see debug messages
    io.set('log level', 1);

    // Listen for incoming connections from clients
    io.sockets.on('connection', function (socket) {

      // Start listening for mouse move events
      socket.on('mousemove', function (data) {

        // This line sends the event (broadcasts it)
        // to everyone except the originating client.
        socket.broadcast.emit('moving', data);
      });
    });

脚本.js

$(function() {

  // This demo depends on the canvas element
  if(!('getContext' in document.createElement('canvas'))){
    alert('Sorry, it looks like your browser does not support canvas!');
    return false;
  }

  // The URL of your web server (the port is set in app.js)
  var url = 'http://localhost:8080';

  var doc = $(document),
  win = $(window),
  canvas = $('#paper'),
  ctx = canvas[0].getContext('2d'),
  instructions = $('#instructions');

  // Generate an unique ID
  var id = Math.round($.now()*Math.random());

  // A flag for drawing activity
  var drawing = false;

  var clients = {};
  var cursors = {};

  var socket = io.connect(url);

  socket.on('moving', function (data) {

    if(! (data.id in clients)){
      // a new user has come online. create a cursor for them
      cursors[data.id] = $('<div class="cursor">').appendTo('#cursors');
    }

    // Move the mouse pointer
    cursors[data.id].css({
      'left' : data.x,
      'top' : data.y
    });

    // Is the user drawing?
    if(data.drawing && clients[data.id]){

      // Draw a line on the canvas. clients[data.id] holds
      // the previous position of this user's mouse pointer

      drawLine(clients[data.id].x, clients[data.id].y, data.x, data.y);
    }

    // Saving the current client state
    clients[data.id] = data;
    clients[data.id].updated = $.now();
  });

  var prev = {};

  canvas.on('mousedown',function(e){
    e.preventDefault();
    drawing = true;
    prev.x = e.pageX;
    prev.y = e.pageY;

    // Hide the instructions
    instructions.fadeOut();
  });

  doc.bind('mouseup mouseleave',function(){
    drawing = false;
  });

  var lastEmit = $.now();

  doc.on('mousemove',function(e){
    if($.now() - lastEmit > 30){
      socket.emit('mousemove',{
        'x': e.pageX,
        'y': e.pageY,
        'drawing': drawing,
        'id': id
      });
      lastEmit = $.now();
    }

    // Draw a line for the current user's movement, as it is
    // not received in the socket.on('moving') event above

    if(drawing){

      drawLine(prev.x, prev.y, e.pageX, e.pageY);

      prev.x = e.pageX;
      prev.y = e.pageY;
    }
  });

  // Remove inactive clients after 10 seconds of inactivity
  setInterval(function(){

    for(ident in clients){
      if($.now() - clients[ident].updated > 10000){

        // Last update was more than 10 seconds ago.
        // This user has probably closed the page

        cursors[ident].remove();
        delete clients[ident];
        delete cursors[ident];
      }
    }

  },10000);

  function drawLine(fromx, fromy, tox, toy){
    ctx.moveTo(fromx, fromy);
    ctx.lineTo(tox, toy);
    ctx.stroke();
  }

});

当我把它放在我的服务器上时,它只有在我在同一个浏览器上打开两个页面时才有效,但在其他计算机上的朋友不可用。我认为这是因为我在代码上对 localhost 进行了调用。但是,我不知道用什么替换它们。

我的服务器上安装了所有节点包,并且以与在 localhost 上启动它的方式相同的方式启动它:

node assets/js/app.js

我得到的输出是:

node assets/js/app.js 
   info  - socket.io started
   warn  - error raised: Error: listen EADDRINUSE

那个警告,我没有在本地机器上得到它。

4

2 回答 2

1

使用服务器 IP 地址而不是 localhost。

在 app.js 中app.listen(port, ip)

在 script.js 中var url = 'http://<ip>:8080';

于 2012-09-29T10:45:06.493 回答
1

我不是节点人。我建议你看:Node / Express: EADDRINUSE, Address already in use - Kill server

EADDRINUSE 可能意味着某些东西已经在计算机上的同一端口/同一接口上运行。看看答案,他们建议你寻找旧的节点实例,如果那不起作用。只需尝试绑定除 8080 之外的另一个端口。

是的,正如@vinayr 所说,不要在本地主机上绑定,您的客户端只会尝试连接本地连接。

于 2012-09-29T10:50:30.357 回答