我刚刚使用socket.io创建了node js app
对于我在本地它工作正常,但当我上传到bluemix时,它给了我错误
以下是在localhost上正常工作的文件
app.js文件
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
res.render('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function() {
console.log( 'A user disconnected' );
});
socket.on('chat message', function(msg) {
console.log('Chat message ' + msg);
var response = {
'msg': msg,
'address': socket.request.connection.remoteAddress
};
io.emit('chat message', JSON.stringify( response ));
});
});
http.listen(1234, '0.0.0.0', function(){
console.log('listening on *:1234');
});
和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="/socket.io/socket.io.js"></script>
<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( info ) {
var response = JSON.parse( info );
$('#messages').append($('<li>').text( response.address + ': ' + response.msg ) );
});
</script>
</body>
</html>
这是服务器上的文件
app.js file
/*eslint-env node*/
//------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
res.render('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function() {
console.log( 'A user disconnected' );
});
socket.on('chat message', function(msg) {
console.log('Chat message ' + msg);
var response = {
'msg': msg,
'address': socket.request.connection.remoteAddress
};
io.emit('chat message', JSON.stringify( response ));
});
});
index.html文件相同
我在谷歌Chrome控制台上出现了这个错误
GET http://testingchat.mybluemix.net/socket.io/?EIO=3&transport=polling&t=1440575370819-112 404 (Not Found)
我使用了此tutorial
中的代码谁能帮帮我? 谢谢
答案 0 :(得分:2)
根据你的代码设置socket.io监听1234端口。 在Bluemix运行时,仅启用端口80,443和9080进行路由。此外,运行时引擎将以动态方式分配您的本地端口,您必须在使用它设置socket.io之前从vcap服务中读取它。 查看Bluemix文档以了解如何操作。
关于404错误,这是由于对/socket.io/ path
的请求答案 1 :(得分:0)
您是否在依赖项中将package.io模块添加到package.json?
"dependencies": {
"express": "^4.6.1",
"socket.io": "^1.3.4"
}
此外,尝试从终端运行 cf logs 命令,以确切了解运行应用时发生的情况。