我尝试运行测试套接字。请帮我找错。感谢您的帮助:)
我在打开的php.ini套接字中使用windows(wamp服务器),安装了nodejs-win。 在cmd.exe中我编写"节点socket.js",回答是" info - socket.io启动",但当我重新加载" localhost"页面,我看到以下错误:
"NetworkError: 404 Not Found - http://localhost/socket.io/socket.io.js"
socket.io.js ReferenceError: io is not defined var socket = io.connect('http://localhost:8080');
如果我运行" localhost:8080",加载将持续多年......
我的档案: 1)soket.js:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
2)index.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>