我试图让socket.io在转移到SSL后再次运行。设置SSL证书并将http:
的所有代码库内部链接更改为https:
后,代码开始抛出错误:
Unable to get property 'emit' of undefined or null reference
我检查了控制台,发现index.html文件中的socket.io的引用(<script src="/socket.io/socket.io.js"></script>
,与官方socket.io网站上列出的相同,现在生成了404.我跑了{ {1}},它是0.9,并运行npm remove socket.io
,以安装当前版本,2.0.3。同样的错误。然后我尝试将socket.io.js文件复制到项目lib文件中,我知道可以公开访问。
将此复制到已知的可公开访问的目录后,我不断更新控制台错误,每个都是以下版本略有不同的版本:
npm install socket.io
然后我为这个CDN切换了/ lib本地参考:
polling-xhr.js:264 XHR finished loading: GET "https://www.cubicverse.com/socket.io/?EIO=3&transport=polling&t=LpqO5Q2".
现在我得到了相同的轮询错误,偶尔散布在这个错误中:
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
在我编辑安装SSL之后,server.js代码现在看起来像这样:
polling-xhr.js:264 GET https://www.cubicverse.com/socket.io/?EIO=3&transport=polling&t=LpqOPOQ 404 (Not Found)
i.create @ polling-xhr.js:264
i @ polling-xhr.js:165
o.request @ polling-xhr.js:92
o.doPoll @ polling-xhr.js:122
r.poll @ polling.js:118
r.doOpen @ polling.js:63
r.open @ transport.js:80
r.open @ socket.js:246
r @ socket.js:120
r @ socket.js:29
r.open.r.connect @ manager.js:226
(anonymous) @ manager.js:540
我如何找出导致此错误的原因,因为似乎socket.io的子库调用了错误的网址,但显然我不想直接编辑此错误(或者至少,我会假设不是)?也许代码库中可能还有其他东西导致通过String.prototype.endsWith = function (suffix)
{
return (this.indexOf(suffix, this.length - suffix.length) !== -1);
}
String.prototype.startsWith = function (prefix)
{
return (this.substr(0, prefix.length) == prefix);
}
String.prototype.contains = function (toSearch)
{
return (this.indexOf(toSearch) != -1);
}
Array.prototype.inArray = function (obj)
{
for (var i = 0; i < this.length; i++)
if (this[i] == obj)
return true;
return false;
}
Array.prototype.append = function (arr)
{
for (var i = 0; i < arr.length; i++)
this[this.length] = arr[i];
}
String.prototype.leftPad = function (char, length)
{
var result = this;
while (result.length < length)
{
result = char + result;
}
return result;
}
String.prototype.hashPassword = function (salt)
{
var crypto = require('crypto');
return crypto.createHash('sha256').update(salt + this).digest('base64');
}
var nextPlayerId = 1;
var app = require('http').createServer(handler),
fs = require('fs'),
privateKey = fs.readFileSync('./server.key', 'utf8'),
certificate = fs.readFileSync('./server.crt', 'utf8'),
credentials = {key: privateKey, cert: certificate},
httpsapp = require('https').createServer(credentials, handler),
url = require("url"),
io = require('socket.io').listen(app, { log: true }),
path = require("path");
var zlib = require('zlib');
var config = require('./config');
GLOBAL.secureKey = config.secureKey;
io.enable('browser client minification');
io.sockets.on('connection', socketHandler);
var knownExt = [
['.html', 'text/html'],
['.js', 'text/javascript'],
['.css', 'text/css'],
['.png', 'image/png'],
['.jpg', 'image/jpeg'],
['.gif', 'image/gif'],
['.mp3', 'audio/mpeg'],
['.ogg', 'audio/ogg'],
['.dae', 'text/plain'],
['.txt', 'text/plain']
];
var moduleDirs = ['modules/', 'admin/', 'object_codes/', 'block_codes/', '3dgeometry/'];
var worldGenModuleDirs = ['special_areas/', 'world_details/'];
GLOBAL.getDb = function ()
{
var mongodb = require("mongodb"),
mongoserver = new mongodb.Server("localhost", 27017, {});
return new mongodb.Db(config.dbName, mongoserver, {safe: true});
}
GLOBAL.after_login = new Array();
GLOBAL.after_disconnect = new Array();
GLOBAL.socketFunctions = new Array();
GLOBAL.socketFunctions[GLOBAL.socketFunctions.length] = {name: 'ping', action: function (socket, data)
{
socket.pingDate = new Date();
}};
GLOBAL.socketFunctions[GLOBAL.socketFunctions.length] = {name: 'position', action: socketPositionUpdate};
GLOBAL.socketFunctions[GLOBAL.socketFunctions.length] = {name: 'disconnect', action: function (socket, data)
{
var playerId = socket.playerId;
//socket.broadcast.emit('player_disconnect', {playerId: socket.playerId});
// Remove useless sockets
for (var i = 0; i < GLOBAL.sockets.length; i++)
{
if (playerId == GLOBAL.sockets[i].playerId)
{
GLOBAL.sockets.splice(i, 1);
break;
}
}
/*for (var i = 0; i < GLOBAL.sockets.length; i++)
GLOBAL.sockets[i].emit('player_disconnect', {playerId: playerId});*/
socket.broadcast.emit('player_disconnect', {playerId: playerId});
for (var i = 0; i < GLOBAL.after_disconnect.length; i++)
GLOBAL.after_disconnect[i](socket);
}};
setInterval(checkTimeout, 1000);
GLOBAL.updatePositionFunctions = new Array();
GLOBAL.specialUri = new Array();
GLOBAL.sockets = new Array();
require('./user_storage.js');
require('./map_storage.js');
require('./general_data.js');
require('./wiki.js');
require('./chat.js');
require('./chat_bot.js');
require('./chat_bot_sentences.js');
require('./player_market.js');
require('./player_owned_areas.js');
require('./news.js');
require('./books.js');
require('./payment_done.js');
var baseDir = process.cwd();
if (baseDir.endsWith('server'))
{
baseDir = path.resolve(baseDir, "..");
}
//console.log(baseDir);
function handler(request, response)
{
var uri = url.parse(request.url).pathname;
if (uri == "/")
uri = "/index.html"
else if (uri == "/chat" || uri == "/chat/")
uri = "/chat/index.html";
for (var i = 0; i < GLOBAL.specialUri.length; i++)
{
if (uri.substr(0, GLOBAL.specialUri[i].name.length) == GLOBAL.specialUri[i].name)
{
GLOBAL.specialUri[i].action(request, response);
return;
}
}
var filename = path.join(baseDir, uri);
if (uri == "/index.html")
{
mainFile(request, response);
return;
}
if (uri == "/welcome.html")
{
welcomeFile(request, response);
return;
}
else if (uri == "/allModulesCode.js")
{
modulesCode(request, response);
return;
}
/*else if (uri == "/cv.appcache")
{
manifestFile(request, response);
return;
}*/
fs.exists(filename, function (exists)
{
var isOk = true;
try
{
if (!exists)
isOk = false;
else if (fs.statSync(filename).isDirectory())
isOk = false;
}
catch (err)
{
isOk = false;
}
if (uri.startsWith("/server/"))
isOk = false;
if (uri.contains("..") || uri.contains("./") || uri.contains("/."))
isOk = false;
if (!isOk)
{
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
for (var i = 0; i < knownExt.length; i++)
{
if (filename.endsWith(knownExt[i][0]))
{
try
{
if (filename.endsWith('.js') || filename.endsWith('.html'))
{
response.writeHead(200, {"Content-Type": knownExt[i][1], "Expires": -1, "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache"});
fs.createReadStream(filename).pipe(response);
}
else if (filename.endsWith('.css'))
{
response.writeHead(200, {"Content-Type": knownExt[i][1], "Expires": -1, "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache"});
cssCode(filename, response);
}
else
{
response.writeHead(200, {"Content-Type": knownExt[i][1], "Cache-Control": "max-age=31536000", "Cache-Control": "public", "Expires": new Date(((new Date()).getTime() + 1000 * 31536000))});
fs.createReadStream(filename).pipe(response);
}
}
catch
(ex)
{
console.log(ex)
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
}
return;
}
}
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
}
);
}
function modulesCode(request, response)
{
response.writeHead(200, { "Content-Type": "text/javascript", "Expires": -1, "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache" });
response.write("var codeLines=[];");
var nbLines = 1;
var allDirs = [];
for (var j = 0; j < moduleDirs.length; j++)
allDirs[allDirs.length] = moduleDirs[j];
for (var j = 0; j < worldGenModuleDirs.length; j++)
allDirs[allDirs.length] = worldGenModuleDirs[j];
for (var j = 0; j < allDirs.length; j++)
{
var files = fs.readdirSync(baseDir + '/' + allDirs[j]);
for (var i = 0; i < files.length; i++)
{
if (files[i].endsWith(".js"))
{
response.write("\n// ----------------------------\n");
response.write("// " + allDirs[j] + "/" + files[i] + "\n");
nbLines += 4;
var data = fs.readFileSync(baseDir + '/' + allDirs[j] + "/" + files[i], "utf8");
var dataLines = data.split('\n').length;
response.write("codeLines[codeLines.length]={fromLine:" + nbLines + ",toLine:" + (nbLines + dataLines) + ",name:'" + allDirs[j] + "/" + files[i] + "'}\n");
nbLines += dataLines;
response.write("// ----------------------------\n");
response.write(data);
}
}
}
response.write("\n\n// ----------------------------\n");
response.write("// images dates\n");
response.write("// ----------------------------\n");
response.write("var imageVersion={};\n");
var contentDirs = ['/images', '/3dmodels'];
for (var j = 0; j < contentDirs.length; j++)
{
var files = fs.readdirSync(baseDir + contentDirs[j]);
for (var i = 0; i < files.length; i++)
{
var s = fs.statSync(baseDir + contentDirs[j] + '/' + files[i]);
response.write("imageVersion['" + files[i] + "']='" + s.mtime.getTime() + "';\n");
}
}
response.write("\n// ----------------------------\n");
response.write("// Init\n");
response.write("initialize();");
/*var code = "";
code += "var codeLines=[];";
var nbLines = 1;
var allDirs = [];
for (var j = 0; j < moduleDirs.length; j++)
allDirs[allDirs.length] = moduleDirs[j];
for (var j = 0; j < worldGenModuleDirs.length; j++)
allDirs[allDirs.length] = worldGenModuleDirs[j];
//var path = require('path');
var mustUpdated = true;
if (fs.existsSync(baseDir + '/cache/allModulesCode.js'))
{
mustUpdated = false;
var cacheFile = fs.statSync(baseDir + '/cache/allModulesCode.js').mtime.getTime();
for (var j = 0; j < allDirs.length && mustUpdated == false; j++)
{
var files = fs.readdirSync(baseDir + '/' + allDirs[j]);
for (var i = 0; i < files.length; i++)
{
if (files[i].endsWith(".js"))
{
if (fs.statSync(baseDir + '/' + allDirs[j] + "/" + files[i]).mtime.getTime() >= cacheFile)
{
mustUpdated = true;
break;
}
}
}
}
}
if (!mustUpdated)
{
//response.write(fs.readFileSync(baseDir + '/cache/allModulesCode.js', "utf8"));
response.write(fs.readFileSync(baseDir + '/cache/allModulesCode.js.orig', "utf8"));
response.end();
return;
}
for (var j = 0; j < allDirs.length; j++)
{
var files = fs.readdirSync(baseDir + '/' + allDirs[j]);
for (var i = 0; i < files.length; i++)
{
if (files[i].endsWith(".js"))
{
code += "\n// ----------------------------\n";
code += "// " + allDirs[j] + "/" + files[i] + "\n";
nbLines += 4;
var data = fs.readFileSync(baseDir + '/' + allDirs[j] + "/" + files[i], "utf8");
var dataLines = data.split('\n').length;
code += "codeLines[codeLines.length]={fromLine:" + nbLines + ",toLine:" + (nbLines + dataLines) + ",name:'" + allDirs[j] + "/" + files[i] + "'}\n";
nbLines += dataLines;
code += "// ----------------------------\n";
code += data;
}
}
}
code += "\n\n// ----------------------------\n";
code += "// images dates\n";
code += "// ----------------------------\n";
code += "var imageVersion={};\n";
var files = fs.readdirSync(baseDir + '/images');
for (var i = 0; i < files.length; i++)
{
var s = fs.statSync(baseDir + '/images/' + files[i]);
code += "imageVersion['" + files[i] + "']='" + s.mtime.getTime() + "';\n";
}
code += "\n// ----------------------------\n";
code += "// Init\n";
code += "initialize();";
var UglifyJS = require("uglify-js");
fs.writeFile(baseDir + '/cache/allModulesCode.js.orig', code+"\n\ncodeStyle='orig'\n", "utf8");
var result = UglifyJS.minify(code+"\n\ncodeStyle='mini'\n", {fromString: true});
var miniCode = result.code;
fs.writeFile(baseDir + '/cache/allModulesCode.js', miniCode, "utf8");
response.write(code+"\n\ncodeStyle='orig'\n");
//response.write(miniCode);*/
response.end();
}
function cssCode(filename, response)
{
//fs.createReadStream(filename).pipe(response);
//console.log(filename)
fs.readFile(filename, "utf8", function (err, fileData)
{
fileData = fileData;
var files = fs.readdirSync(baseDir + '/images');
for (var i = 0; i < files.length; i++)
{
var r = new RegExp(files[i].replace(".", "\\."), "g");
var s = fs.statSync(baseDir + '/images/' + files[i]);
fileData = fileData.replace(r, files[i] + "?v=" + s.mtime.getTime());
}
//console.log(fileData)
response.write(fileData);
response.end();
});
}
function mainFile(request, response)
{
var filename = path.join(baseDir, "/index.html");
//console.log(filename);
fs.readFile(filename, function (err, fileData)
{
response.writeHead(200, { "Content-Type": "text/html", "Expires": -1, "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache" });
var worldGenModules = [];
for (var j = 0; j < worldGenModuleDirs.length; j++)
{
var files = fs.readdirSync(baseDir + '/' + worldGenModuleDirs[j]);
for (var i = 0; i < files.length; i++)
{
if (files[i].endsWith(".js"))
{
worldGenModules[worldGenModules.length] = worldGenModuleDirs[j] + files[i];
//modules[modules.length] = worldGenModuleDirs[j] + files[i];
}
}
}
//response.write(("" + fileData).replace("#title#", config.title).replace("'#modules#'", JSON.stringify(modules)).replace("'#worldGenModules#'", JSON.stringify(worldGenModules)));
response.write(("" + fileData).replace("#title#", config.title).replace("'#worldGenModules#'", JSON.stringify(worldGenModules)));
response.end();
});
}
function sortByDate(a, b)
{
if (a.date < b.date)
return 1;
if (b.date < a.date)
return -1;
return 0;
}
/*function manifestFile(request, response)
{
response.writeHead(200, { "Content-Type": "text/cache-manifest", "Expires": -1, "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache" });
var dirs = [];
dirs.append(moduleDirs);
dirs.append(worldGenModuleDirs);
dirs.append(["images/"]);
var maxfs = 0;
for (var j = 0; j < dirs.length; j++)
{
//console.log(baseDir + "/" + dirs[j])
var files = fs.readdirSync(baseDir + "/" + dirs[j]);
for (var i = 0; i < files.length; i++)
{
var s = fs.statSync(baseDir + '/' + dirs[j] + files[i]);
var s = s.mtime.getTime();
if (s > maxfs)
maxfs = s;
}
}
response.write("CACHE MANIFEST\n");
response.write("# Version " + s + "\n");
for (var j = 0; j < dirs.length; j++)
{
//console.log(baseDir + "/" + dirs[j])
var files = fs.readdirSync(baseDir + "/" + dirs[j]);
for (var i = 0; i < files.length; i++)
{
var s = fs.statSync(baseDir + '/' + dirs[j] + files[i]);
var s = s.mtime.getTime();
if (s > maxfs)
maxfs = s;
}
}
response.write("/index.html\n");
response.write("/socket.io/socket.io.js\n");
response.write("/lib/random.js\n");
response.write("/lib/perlin.js\n");
response.write("/main.js\n");
response.write("/allModulesCode.js\n");
for (var j = 0; j < dirs.length; j++)
{
//console.log(baseDir + "/" + dirs[j])
var files = fs.readdirSync(baseDir + "/" + dirs[j]);
for (var i = 0; i < files.length; i++)
{
if (files[i].endsWith(".css"))
response.write('/' + dirs[j] + files[i] + '\n');
}
}
var files = fs.readdirSync(baseDir + "/images");
for (var i = 0; i < files.length; i++)
{
var s = fs.statSync(baseDir + '/images/' + files[i]);
var s = s.mtime.getTime();
response.write('/images/' + files[i] + '?v=' + s + '\n');
}
response.end();
}*/
function welcomeFile(request, response)
{
var filename = path.join(baseDir, "/welcome.html");
//console.log(filename);
fs.readFile(filename, function (err, fileData)
{
response.writeHead(200, { "Content-Type": "text/html", "Expires": -1, "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache" });
var db = GLOBAL.getDb();
db.open(function (err, db)
{
if (db == null)
{
fileData = ("" + fileData).replace("#news#", "");
response.write(fileData.replace("#top#", ""));
return;
}
db.collection("news", function (err, collection)
{
collection.find().toArray(function (err, data)
{
var news = "<ul>";
data.sort(sortByDate);
for (var i = 0; i < data.length && i < 5; i++)
news += "<li><b>" + data[i].date + ":</b> " + data[i].text + "</li>";
news += "</ul>";
fileData = ("" + fileData).replace("#news#", news);
db.collection("top", function (err, collection)
{
if (collection == null)
{
response.write(fileData.replace("#top#", ""));
response.end();
db.close();
return;
}
collection.findOne({_id: "user"}, function (err, data)
{
if (data == null)
{
response.write(fileData.replace("#top#", ""));
response.end();
db.close();
return;
}
var top = "<table>";
top += "<tr><td>Best players:</td><td>Level:</td></tr>";
for (var i = 0; i < data.level.length && i < 15; i++)
{
top += "<tr><td>" + data.level[i].username + "</td><td>" + data.level[i].value + "</td></tr>";
}
top += "</table>";
response.write(fileData.replace("#top#", top));
response.end();
db.close();
return;
});
});
});
});
});
});
}
function socketHandler(socket)
{
GLOBAL.sockets[GLOBAL.sockets.length] = socket;
socket.playerId = nextPlayerId++;
socket.pingDate = new Date();
socket.broadcast.emit('player_connect', {playerId: socket.playerId});
socket.emit('player_id', {playerId: socket.playerId});
for (var i = 0; i < GLOBAL.socketFunctions.length; i++)
eval("socket.on(GLOBAL.socketFunctions[" + i + "].name,function (data) { GLOBAL.socketFunctions[" + i + "].action(socket,data); });");
}
function socketPositionUpdate(socket, data)
{
//console.log("ID: "+data.playerId+", X: "+data.x+", Y: "+data.y+" Z: "+data.z);
// Send players within the areas
for (var i = 0; i < GLOBAL.sockets.length; i++)
{
if (GLOBAL.sockets[i].playerId == socket.playerId)
continue;
if (GLOBAL.sockets[i].position == undefined && Math.abs(data.ax) < 2 && Math.abs(data.ay) < 2)
{
GLOBAL.sockets[i].emit('player_position', data);
continue;
}
if (GLOBAL.sockets[i].position == undefined || GLOBAL.sockets[i].position.ax == null)
continue;
if (Math.abs(GLOBAL.sockets[i].position.ax - data.ax) > 1 || Math.abs(GLOBAL.sockets[i].position.ay - data.ay) > 1)
continue;
GLOBAL.sockets[i].emit('player_position', data);
}
socket.position = data;
for (var i = 0; i < GLOBAL.updatePositionFunctions.length; i++)
GLOBAL.updatePositionFunctions[i](socket, data);
}
function checkTimeout()
{
/*var now = new Date();
for (var i = 0; i < GLOBAL.sockets.length; i++)
{
var diff = (now - GLOBAL.sockets[i].pingDate) / 1000;
if (diff > 60 && GLOBAL.sockets[i].username != null && GLOBAL.sockets[i].username != undefined)
{
GLOBAL.sockets[i].emit('close_duplicate', {});
GLOBAL.sockets[i].disconnect('unauthorized');
}
}*/
}
app.listen(config.port);
httpsapp.listen(config.httpsport);
库调用此URL?如果是这样,有人可能会建议一个grep命令或类似的东西可能允许我隔离它吗?在任何主服务器文件中都没有任何明显的结果,并且手动传输超过70,000行代码并不是真正的选择。还有什么其他方法可以解决这个错误吗?