【发布时间】:2018-03-20 15:23:21
【问题描述】:
我有一个完全正常工作的生产服务器,其设置如下: NodeJS 在 3000 端口上配置了 Nginx 和 SSL。ExpressJS 在前面。
现在我正在尝试在里面添加一些套接字工作。 我使用以下代码创建了一个名为 server.js 的新文件:
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 6969;
net.createServer(function(sock) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
sock.write('You said "' + data + '"');
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
sock.on('error', function(err) {
console.log(err)
})
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
我在我的主 app.js 文件中添加了这一行:
require('./app/server');
现在,当我启动服务器时,它会按预期打印以下行: “套接字服务器正在侦听 127.0.0.1:6969” 和 "NodeJS 生产服务器监听端口 3000"
现在,当我尝试通过以下方式访问套接字服务器时:
netcat [destination] 6969
它不起作用。 但是当我在没有 SSL 和 Nginx 的 localhost 开发服务器上尝试它时,它确实有效。
可能我必须添加一些 Nginx 配置或安全连接才能看到套接字,但我在网上找不到任何线索。
任何帮助将不胜感激!
【问题讨论】: