Node.js聊天应用程序,Socket.io 和Express在本地工作,但不在本地托管



我做了 socket.io 的"入门"示例,它在我的计算机上的本地主机上完美运行。但是当我将其上传到我的主机时,它不起作用,客户端在尝试获取 socket.io 时抛出 404 错误.js:

<script src="/socket.io/socket.io.js"></script>

我用这个其他参考解决了这个问题(我不需要在 localhost 上这样做(:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>

但是,当客户端在主机上运行时,我不断收到 404 错误。似乎 Node.js 服务器未在服务器上运行,并且在托管时不提供 socket.io 文件。当我在本地主机上时,我没有收到这些 404 错误。有一个调用的示例:

GET http://localhost/socket.io/?EIO=3&transport=polling&t=MMc5E4X(200 在本地工作正常(

GET http://tinatest.gearhostpreview.com/socket.io/?EIO=3&transport=polling&t=MMc5E4X(404(

我在免费的 Node.js 主机上得到了这个示例,所以你可以看看它,我在此服务器上激活了 Node.js 8,这在端口 80 上运行:

http://tinatest.gearhostpreview.com/

服务器上的文件是:

索引.html

<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
</html>

索引.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(80, function(){
console.log('listening on *:80');
});

包.json

{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.15.2",
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1"
}
}

我怎样才能做到这一点?或者如果不是,为什么不可能?

想想Node.js+express服务器的作用。在许多事情中,它可以根据各种类型的http请求提供文件。但是,你必须准确地告诉它要侦听什么类型的请求(GETPOST等(以及在什么路由上(//folder(。您甚至可以侦听查询字符串、参数、帖子消息正文等。但是,由您来定义它们。

查看您的代码,您定义了什么路由?

只有一个:/,你允许GET请求到该路由,作为响应,你正在为index.html提供服务,它有你所有的前端逻辑,至少在这种情况下是这样。

如果您尝试GET其他路由(如/socket.io/默认情况下,您将收到 404 错误。

您提到的脚本文件具有src/socket.io,因为您没有提供 socket.io 代码的路由,因此它在生产中失败。

在本地主机上,如果文件在同一项目中可用,则浏览器可以解析本地计算机上的相对路径。部署项目后,相对路径不再指向您的计算机,而是使用完整的 url 访问您的路由器。它期望提供这些文件。

您可以通过为必要的socket.io代码提供完整的 url 引用来解决生产中的问题。

更多:一个例子:

index.js- 请参阅下面的评论

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
/**
* Your ROUTES
* in production, when someone goes to http://tinatest.gearhostpreview.com/
* they are calling your root route "/"
* Your callback function is sending back index.html from your root directory
* You add __dirname because you don't know where gearhost is actually putting
* your stuff. 
*
* In production, you can only access server-side files if you serve them
*/
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
/** 
* ONE WAY TO SERVE JS TO YOUR CLIENT 
* (there are many better ways of doing this)
*
* If you do this, you will not get a 404 error
*/
app.get('/socket.io/socket.io.js', function(req, res){
// this assumes you have a folder within your root named socket.io 
// and that it contains socket.io.js, 
res.sendFile(__dirname + '/socket.io/socket.io.js');
})

io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(80, function(){
console.log('listening on *:80');
});

index.html- 只看脚本

<!-- 
The src is the URI of an external script, that is, 
a script external to this particular document.
Below, the URIs are absolute paths for getting for files hosted by other services.
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<!-- 
You can also use relative paths.
If your src value is /socket.io, 
then your page looks for a file
relative to the current location of itself.
On localhost, your page has access to files within its own folder
-->
<script src="/socket.io/socket.io.js"></script>
<!--
HOWEVER, in production, you cannot use relative paths
UNLESS you are serving your files either as static assets
via middleware (http://expressjs.com/en/guide/using-middleware.html)
OR via a public route (http://expressjs.com/en/4x/api.html#app.use)
You are getting a 404 in production because you aren't serving
assets.
BUT!!! you can always use an external 3rd party service
-->

正如我一直在想的那样,问题是我没有运行任何节点服务器。我遵循了以下步骤,在我使用的是免费非专用主机(没有远程桌面(的情况下,我使用了 VS Code:

  1. 将我的主机更改为Windows VPS(带远程桌面(。
  2. 安装节点.js在那里。
  3. 创建单独的 socket.io 服务器应用。
  4. 使用代码 cmd 在本地主机:8080 中使用node index运行服务器。
  5. 从窗口控制面板安装的 iis。
  6. 创建指向服务器:VPS_IP_ADRESS:8080的单独客户端代码
  7. 构建客户端并将 dist 文件夹内容复制到 IIS 根文件夹(很可能是C:inetpubwwroot
  8. (
  9. 运行 IIS。

现在,在任何设备上,我的VPS IP中一切都可以完美运行。我可以在我的设备之间聊天。我使用IIS作为客户端,因为我曾经在那里发布。我想我不能以任何方式在我的旧主机中做到这一点,因为我要求 Node 支持,他们告诉我他们没有。Ubuntu VPS应该是一个更便宜的选择,但现在对我来说很重要。如果你对Node + Ubuntu服务器有经验,你应该尝试。

  • 我将在几个小时内用最终的生产代码(隐藏我的 IP(编辑这些代码

最新更新