我在Netlify上有应用,但是我收到了一些错误: 服务器不工作:9aeacd2 ... .js:1 WebSocket连接到' wss:// xxxxxxxxxxxxx /:5000' failed:在收到握手响应之前关闭连接。仅提供开始网站。 在当地所有工作都很完美,我没有得到任何错误。应用程序建立在Netlify(gulp)上,这是一个有角度的应用程序。 帮助我,谢谢!
我的服务器代码:
"use strict";
process.title = 'node-chat';
var webSocketsServerPort = (process.env.PORT || 5000);
var webSocketServer = require('websocket').server;
var http = require('http');
var pref = '../';
var fs = require('fs');
var index = fs.readFileSync('src/index.html');
var clients = [];
var loggedUsers = [];
var tables = [];
var err;
/**
* HTTP server
*/
var server = http.createServer(function (req, res) {
var status;
var type;
var file = null;
switch (req.url) {
case "/":
case "/src/index.html":
file = index;
status = 200;
type = "text/html";
break;
default:
status = 404;
type = "text/plain";
}
res.writeHead(status, {'Content-Type': type});
if (file !== null) {
res.end(file);
} else {
res.end();
}
});
server.listen(webSocketsServerPort, function () {
console.log((new Date()) + " Server is listening on port " +
webSocketsServerPort);
});
/**
* WebSocket server
*/
var wsServer = new webSocketServer({
httpServer: server
});
wsServer.on('request', function (request) {
console.log((new Date()) + ' Connection from origin ' + request.origin +
'.' );
var connection = request.accept(null, request.origin);
var index = clients.push(connection) - 1;
connection.on('message', function (message) {
if (message.type === 'utf8') { // accept only text
var json = JSON.parse(message.utf8Data);
switch(json.type){
case 1:
break;
}
}
});
// user disconnected
connection.on('close', function (connection) {
console.log("DISCONNECTED");
});
});