【问题标题】:socket.io chat example herokusocket.io 聊天示例 heroku
【发布时间】:2016-10-23 08:26:28
【问题描述】:

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

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

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"
  }
}

过程文件

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

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

【问题讨论】:

    标签: node.js heroku


    【解决方案1】:

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

    混合内容:位于“https://salty-ridge-74778.herokuapp.com/”的页面 已通过 HTTPS 加载,但请求了不安全的脚本 'http://code.jquery.com/jquery-1.11.1.js'。该请求已 受阻;内容必须通过 HTTPS 提供。 salty-ridge-74778.herokuapp.com/:25 Uncaught ReferenceError: $ is not 定义

    而不是包含这样的脚本,您可以将它们的协议硬编码为安全或不安全:

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2016-12-20
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      相关资源
      最近更新 更多