请您查看下面的代码,让我知道我在做错了什么?在Ubuntu 12.4上运行Node.js + Express + Socket.io我试图创建一个简单的Push函数。服务器工作正常,但我无法在页面上看到div id“status”内容,显然该功能没有运行!
感谢您提前给予帮助和评论,对不起,感谢对不起![/ p>
以下是 app.js 文件 / ** *模块依赖性。 * /
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
var status = "I am not changed yet!.";
io.sockets.on('connection', function (socket) {
io.sockets.emit('status', { status: status });
socket.on('reset', function (data) {
status = "You Change the text!";
io.sockets.emit('status', { status: status });
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我修改了“ package.json ”,如下所示,将sockt.io添加到依赖项
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.2.3",
"jade": "*",
"socket.io:0.9.14"
}
}
视图文件夹中的和 index.jade 我
<div id="status"></div>
<button id="reset">Reset!</button>
并在 layout.jade 我拥有的是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="description" content="">
<meta name="author" content="">
<!-- HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- styles -->
<link href="/css/main.css" rel="stylesheet">
</head>
<body>
<%- body %>
<script src="node_modules/socket.io/socket.io.js"></script>
<script src="public/js/libs/jquery.js"></script>
<script src="public/js/main.js"></script>
</body>
最后这是我在 main.js :
中的功能var socket = io.connect(window.location.hostname);
socket.on('status', function (data) {
$('#status').html(data.status);
});
$('#reset').click(function() {
socket.emit('reset');
});
错误robert Hint编辑后的消息
SyntaxError: Unexpected end of input
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
答案 0 :(得分:2)
你遗漏了一些基础知识:你没有加载socket.io
模块(我假设你确实安装了它,否则先使用npm install socket.io
),你没有将它连接到HTTP服务器,并且您没有正确地将客户端包含在模板中(正如@ltebean已经指出的那样)。
服务器端优先:
// app.js
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express()
, server = http.createServer(app) // create HTTP server
, io = require('socket.io').listen(server); // load socket.io and connect
// it to the HTTP server
...
// start listening
server.listen(app.get('port'));
客户端下一步:
// layout.jade
...
<script src="/socket.io/socket.io.js"></script>
...
在main.js
中,我建议使用io.connect
自动发现:
var socket = io.connect(); // no argument means auto-discovery
此外,您package.json
无效:
"socket.io:0.9.14" // not a valid object property!
"socket.io" : "0.9.14" // should be this