gpt4 book ai didi

node.js - socket.io 聊天示例 heroku

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:18 27 4
gpt4 key购买 nike

我一直在尝试学习node.js和socket.io并完成了http://socket.io/get-started/chat/处的示例。我添加了一些额外的功能,它可以在本地主机上运行。现在我正在尝试将其部署到 heroku 上的服务器,但我无法让它工作。

我没有足够的声誉来显示我读过的主要内容。我查看了 Heroku 上的文章“在 Heroku 上使用 Node.js 入门”、“在 Heroku 上部署 Node.js 应用程序”和“在 Heroku 上使用 WebSockets 与 Node.js”,但我仍然不知道该怎么做。

html 页面显示在我的应用程序上,但聊天不起作用:https://salty-ridge-74778.herokuapp.com/

这是我到目前为止所拥有的:

index.html

<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
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));
});
socket.on('user connected', function(name){
$('#messages').append($('<li>').text(name + " connected"));
});
</script>
</body>
</html>

index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

var nextUserId = 0;
var users = [];

app.set('port', (process.env.PORT || 5000));

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
var socketId = socket.id;
users.push({ 'id': socketId, 'name': "User" + nextUserId });
nextUserId++;

console.log(users[users.length - 1].name + ' joined with id ' + users[users.length - 1].id);
io.emit('user connected', users[users.length - 1].name);
socket.on('disconnect', function () {
console.log('user disconnected');
});
socket.on('chat message', function (msg) {
var name;
for (var x = 0; x < users.length; x++) {
if (users[x].id == socket.id) {
name = users[x].name;
}
}

io.emit('chat message', name + ": " + msg);
console.log('message: ' + name + ": " + msg);
});
});

http.listen(app.get('port'), function(){
console.log('listening on port ' + app.get('port'));
});

package.json

{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.10.2",
"socket.io": "^1.4.6"
},
"engines": {
"node": "6.2.2"
}
}

Proc文件

web: node index.js

.gitignore

node_modules/

为了设置应用程序,我在进入正确的文件夹后在命令行中输入了这些命令:

git init
heroku create
git add .
git commit -m '1'
heroku git:remote -a salty-ridge-74778
git push heroku master

如果有人能提供帮助,我将永远感激不已。

最佳答案

控制台显示导致您的应用程序失败的 JavaScript 错误。在浏览器中打开调试控制台:

Mixed Content: The page at 'https://salty-ridge-74778.herokuapp.com/' was loaded over HTTPS, but requested an insecure script 'http://code.jquery.com/jquery-1.11.1.js'. This request has been blocked; the content must be served over HTTPS. salty-ridge-74778.herokuapp.com/:25 Uncaught ReferenceError: $ is not defined

不要包含这样的脚本,而是将其协议(protocol)硬编码为安全或不安全:

<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

像这样包含它们,因此它们继承托管页面的协议(protocol):

<script src="//code.jquery.com/jquery-1.11.1.js"></script>

关于node.js - socket.io 聊天示例 heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37950349/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com