刚刚在Heroku上部署了一个应用程序(https://socket-draw-app.herokuapp.com/),但我收到此错误:ReferenceError:进程未定义。在javascript文件中:sketch.js。我正在使用p5.js库。下面的代码(首先是sketch.js,然后是我的server.js节点文件):
var socket;
function setup() {
createCanvas(400, 400);
background(0);
socket = io.connect(process.env.PORT || 3000);
socket.on('mouse',
function(data) {
console.log("Got: " + data.x + " " + data.y);
fill(0,0,255);
noStroke();
ellipse(data.x, data.y, 20, 20);
}
);
}
function draw() {
// Nothing
}
function mouseDragged() {
// Draw some white circles
fill(255);
noStroke();
ellipse(mouseX,mouseY,20,20);
// 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);
}
var express = require('express');
// Create the app
var app = express();
// Set up the server
// process.env.PORT is related to deploying on heroku
var server = app.listen(process.env.PORT || 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");
});
}
);
{
"name": "sockets_p5",
"version": "1.0.0",
"description": "This is an example for how to use sockets with p5.js.",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Daniel Shiffman",
"license": "ISC",
"dependencies": {
"express": "^4.16.2",
"socket.io": "^1.4.5"
}
}
从我运行Ubuntu 16.04的计算机上部署。节点版本4.2.6和Npm版本3.5.2。