0

我正在尝试使用 Node.js、Express.js 和 Socket.IO 部署一个小型协作绘图应用程序。

在本地,它在运行 Node.js 的 MacOS 上完美运行,通过 LAN 和 WAN,但我无法让它在我们的 Plesk 服务器上运行。我按照 Alexei Yuzhakov 的指南在 Plesk 中安装和配置 Node.js:

https://www.plesk.com/blog/product-technology/node-js-plesk-onyx/

我还使用了一些 Node.js 测试应用程序,一切似乎都已配置并正常工作。

然后我安装了我的应用程序并运行 NPM 安装以成功下载依赖项。

但是,当客户端尝试连接时,会出现以下错误:

加载资源失败:请求超时。http://example.com:3000/socket.io/?EIO=3&transport=polling&t=NWEYhCy

不知何故,客户端无法连接到端口 3000 上的套接字

我尝试了各种方法,例如禁用 PHP,并且还删除了 Apache 设置中的代理模式设置复选框。但没有运气。

任何帮助,将不胜感激!

服务器代码(server.js):

// Using express: http://expressjs.com/
var express = require('express');
// Create the app
var app = express();

// Set up the server
var server = app.listen(3000, listen);

// This call back just tells us that the server has started
function listen() {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://' + host + ':' + port);
}

app.use(express.static('public'));


// WebSocket Portion
// WebSockets work with the HTTP server
var io = require('socket.io')(server);

// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
io.sockets.on('connection',
  // We are given a websocket object in our function
  function (socket) {
  
    console.log("We have a new client: " + socket.id);
  
    // When this user emits, client side: socket.emit('otherevent',some data);
    socket.on('mouse',
      function(data) {
        // Data comes in as whatever was sent, including objects
        console.log("Received: 'mouse' " + data.x + " " + data.y);
      
        // Send it to all other clients
        socket.broadcast.emit('mouse', data);
        
        // This is a way to send to everyone including sender
        // io.sockets.emit('message', "this goes to everyone");

      }
    );
    
    socket.on('disconnect', function() {
      console.log("Client has disconnected");
    });
  }
);

客户代码:

// Keep track of our socket connection
var socket;

function setup() {
  createCanvas(window.innerWidth, window.innerHeight);
  background(250,250,250);
  // Start a socket connection to the server
  // Some day we would run this server somewhere else
  socket = io.connect('http://example.com:3000');
  // We make a named event called 'mouse' and write an
  // anonymous callback function
  socket.on('mouse',
    // When we receive data
    function(data) {
      console.log("Got: " + data.x + " " + data.y);
      // Draw a grey circle
      fill(100,100,100);
      noStroke();
      ellipse(data.x, data.y, 5, 5);
    }
  );
}

function draw() {
  // Nothing
}

function mouseDragged() {
  // Draw some grey circles
  fill(100,100,100);
  noStroke();
  ellipse(mouseX,mouseY,5,5);
  // Send the mouse coordinates
  sendmouse(mouseX,mouseY);
}

// Function for sending to the socket
function sendmouse(xpos, ypos) {
  // We are sending!
  console.log("sendmouse: " + xpos + " " + ypos);
  
  // Make a little object with  and y
  var data = {
    x: xpos,
    y: ypos
  };

  // Send that object to the socket
  socket.emit('mouse',data);
}
4

0 回答 0