简体   繁体   English

无法在浏览器中使用Node.js从网站连接到Websocket

[英]Cannot connect to websocket from website using Nodejs within browser

Has anybody had luck creating a website with real-time communication (chat) using Nodejs and websockets on Fastcomet? 是否有人在Fastcomet上使用Nodejs和websockets通过实时通信(聊天)创建网站? I've submitted many tickets to Fastcomet but we keep going in circles. 我已经向Fastcomet提交了许多票,但是我们一直在圈子里奔波。

My goal is to put something like https://davidwalsh.name/websocket online at www.mywebsite.com/socketio 我的目标是在www.mywebsite.com/socketio上放置类似于https://davidwalsh.name/websocket的内容

All my tests work locally when I open http://127.0.0.1:3000/ in my browser. 当我在浏览器中打开http://127.0.0.1:3000/时,我所有的测试都在本地工作。

I started by setting up a Node.js app from cpanel 我首先从cpanel设置了一个Node.js应用

then fill in the details 然后填写详细信息

This procedure creates a .htaccess file for Phusion Passenger. 此过程为Phusion Passenger创建一个.htaccess文件。

even tried RewriteRule ^(. ) http://localhost:3000/ $1 [P]* 甚至尝试过RewriteRule ^(。http:// localhost:3000 / $ 1 [P] *

When I go to www.mywebsite.com/socketio I get the following error: 当我转到www.mywebsite.com/socketio时,出现以下错误:

http://www.mywebsite:3000/socket.io/?EIO=3&transport=polling&t=1531226905601-0 Failed to load resource: net::ERR_NAME_NOT_RESOLVED http://www.mywebsite:3000 / socket.io /?EIO = 3&transport = polling&t = 1531226905601-0加载资源失败:net :: ERR_NAME_NOT_RESOLVED

Here is another failed test: 这是另一个失败的测试:

Server index.js (Nodejs status: started) 服务器index.js(Nodejs状态:已启动)

var http = require('http');
var server = http.createServer(function(req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   var message = 'It works!\n',
       version = 'NodeJS ' + process.versions.node + '\n',
       response = [message, version].join('\n');
   res.end(response);
});
server.listen();
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
 ws.on('message', function incoming(message) {
  console.log('received: %s', message);
 });
 ws.send('something')
});

client index.html 客户index.html

<html>
<head>
    <script>
        var sock =new WebSocket('ws://' + window.location.host+"/chat" );
    </script>
</head>
<body>
</body>
</html>

even tried 甚至尝试过

var sock =new WebSocket('ws://' + window.location.host+":8080/chat" );

I also changed port 8080 to 3000 for the index.js and index.html 我还将index.js和index.html的端口8080更改为3000

I even removed port from index.js and index.html 我什至从index.js和index.html中删除了端口

Access through browser at: 通过浏览器访问:

http://www.mywebsite.com/chat/index.html http://www.mywebsite.com/chat/index.html

Error: 错误:

WebSocket connection to 'ws://www.mywebsite.com/chat' failed: Error during WebSocket handshake: Unexpected response code: 500 WebSocket与“ ws://www.mywebsite.com/chat”的连接失败:WebSocket握手期间出错:意外的响应代码:500

Anybody have ideas? 有人有想法吗?

Have you tried creating your project outside the public_html folder? 您是否尝试过在public_html文件夹之外创建项目?

Try creating a folder (eg: home/user/socket_test) and put your code inside that folder and point your node.js app to that path. 尝试创建一个文件夹(例如:home / user / socket_test),然后将代码放在该文件夹中,然后将node.js应用程序指向该路径。

Ports are mapped automatically in "Phusion Passenger" meaning whatever port you specify in the program will be automatically reverse mapped by phusion passenger. 端口在“ Phusion Passenger”中自动映射,这意味着您在程序中指定的任何端口都会被Phusion Passenger自动反向映射。

Use port 3000 for node apps, example.com/app will have port of 8080 getting mapped to port 3000. 将3000端口用于节点应用程序,example.com/app会将8080端口映射到3000端口。

I'm using Fastcommet shared hosting plan and running a node express RESTful API and its working very well. 我正在使用Fastcommet共享托管计划,并运行一个节点表达的RESTful API,并且运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM