2016-06-21 3 views
1

나는 node.js와 socket.io를 배우려고했으며 http://socket.io/get-started/chat/에서 예제를 완성했다. 몇 가지 추가 기능을 추가하고 localhost에서 작동합니다. 이제 이것을 heroku의 서버에 배포하려고 시도하고 있지만 제대로 작동하지 않습니다.socket.io chat example heroku

내가 읽은 주요 내용을 보여줄만큼 충분한 평판이 없습니다. heroku에 대한 기사 "Node.js로 Heroku 시작하기", "Heroku에서 Node.js 응용 프로그램 배포"및 "Node.js와 함께 Heroku에서 WebSockets 사용"을 살펴 보았지만 여전히 어떻게 해야할지 알 수 없습니다.

HTML 페이지 내 응용 프로그램에 표시하지만 채팅은 작동하지 않습니다

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> 

지수 : https://salty-ridge-74778.herokuapp.com/ 여기

내가 지금까지 무엇을 가지고 .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>