2016-06-21 91 views
1

我一直在嘗試學習node.js和socket.io,並完成了http://socket.io/get-started/chat/的示例。我添加了一些額外的功能,它在本地主機上工作。現在我試圖將這部署到heroku上的服務器上,但我無法使其工作。socket.io聊天示例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> 

指數.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" 
    } 
} 

Procfile

web: node index.js 

的.gitignore

node_modules/ 

要設置應用了我輸入一次我這些命令到命令行是在正確的摺疊ER:

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

如果有人可以幫助我會感激不盡。

回答

2

控制檯顯示導致您的應用程序失敗的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

而是包括這樣的腳本,在那裏你硬編碼的協議,以安全或不安全的:

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

包括他們這個樣子,所以它們繼承的協議託管網頁:

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