[英]Node.JS - Simple Socket.IO example not working. Getting debug - served static content /socket.io.js (Mac OSX/Localhost)
[英]getting simple node.js socket.io example working
我从以下网址下载了这个非常简单的socket.io示例: https : //github.com/shapeshed/nodejsbook.io.examples/tree/master/hour12/example02
package.json:
{ "name" : "socketio_example"
, "version" : "0.0.1"
, "private" : "true"
, "dependencies" : { "socket.io" : "0.8.7" }
}
app.js:
var http = require('http') ;
var fs = require('fs') ;
var count = 0;
var server = http.createServer(function (req, res) {
fs.readFile('./index.html' , function(error, data) {
res.writeHead(200, { 'Content-Type' : 'text/html'});
res.end(data, 'utf-8');
});
}).listen(3000, "1xx.2xx.1xx.26");
console.log('Server is running');
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
count++;
console.log('User connected; ' + count + ' user(s) present.' );
socket.emit ('users' , { number : count }) ;
socket.broadcast.emit ('users' , { number : count }) ;
socket.on('disconnect', function() {
count--;
console.log('User disconnected; ' + count + ' user(s) present.' );
socket.broadcast.emit('users' , { number : count }) ;
});
});
index.html:
<!DOCTIME html>
<html lang='en'>
<head>
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<p id='count'></p>
<script src='/socket.io/socket.io.js'></script>
<script>
var socket = io.connect('http://1xx.2xx.1xx.26:3000') ;
var count = document.getElementById('count');
socket.on('users', function(data) {
console.log('Got update from the server!');
console.log('There are ' + data.number + ' users!');
count.innerHTML = data.number;
});
<script>
</body>
<html>
然后执行:
node install ;
最后:
node app.js &
然后当我使用本地主机(127.0.0.1)尝试此操作时,我可以通过执行以下操作查看我的html代码:
curl http://127.0.0.1:3000 ;
然后我将IP地址从127.0.0.1更改为自己的IP地址。 并重新启动该应用。 该命令:
curl http://1xx.2xx.1xx.26:3000 ;
再次向我展示了html代码。
该项目应该显示连接数量的计数,但是我似乎无法使其正常工作。 但是,我也没有任何错误。 当我浏览到http://1xx.2xx.1xx.26:3000 /时,网页出现了,标题出现了,但没有其他显示,没有用户计数。
当网页连接时,我在服务器上执行此消息:
debug - served static content /socket.io.js
任何建议或想法我可能做错了什么?
谢谢你们!
我也很头疼,围绕着我在socket.io上看到的一些示例,所以我试图尽可能简单地将其分解。 也许这对您也有帮助。
我从此处发布的示例改编了该示例: http : //socket.io/get-started/chat/
首先,从一个空目录开始,然后创建一个名为package.json的非常简单的文件。
{
"dependencies": {}
}
接下来,在命令行上,使用npm安装此示例所需的依赖项
$ npm install --save express socket.io
这可能需要几分钟,具体取决于网络连接/ CPU /等的速度。要检查一切是否按计划进行,可以再次查看package.json文件。
$ cat package.json
{
"dependencies": {
"express": "~4.9.8",
"socket.io": "~1.1.0"
}
}
创建一个名为server.js的文件,这显然是我们的服务器,由node运行。 将以下代码放入其中:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
//send the index.html file for all requests
res.sendFile(__dirname + '/index.html');
});
http.listen(3001, function(){
console.log('listening on *:3001');
});
//for testing, we're just going to send data to the client every second
setInterval( function() {
/*
our message we want to send to the client: in this case it's just a random
number that we generate on the server
*/
var msg = Math.random();
io.emit('message', msg);
console.log (msg);
}, 1000);
创建最后一个名为index.html的文件,并将以下代码放入其中。
<html>
<head></head>
<body>
<div id="message"></div>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('message', function(msg){
console.log(msg);
document.getElementById("message").innerHTML = msg;
});
</script>
</body>
</html>
现在,您可以测试这个非常简单的示例,并看到一些类似于以下内容的输出:
$ node server.js
listening on *:3001
0.9575486415997148
0.7801907607354224
0.665313188219443
0.8101786421611905
0.890920243691653
如果打开Web浏览器,并将其指向运行节点进程的主机名,则应该在浏览器中看到相同的数字,以及与该页面相同的任何其他连接的浏览器。
我在virtualbox上安装了一个新的linux并玩了。 运行localhost可以正常工作,但不能通过网络运行。
答案很明显:
app.js-
原始的9&10行:
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127 .0.0.1:3000/');
新的第9和10行:
}).listen(3000);
console.log('Server running at port 3000/');
index.html :
原始第13行:
var socket = io.connect('http://127.0.0.1:3000');
第13行:
var socket = io.connect('http://1xx.2xx.3xx.1:3000');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.