socket.io库客户端引用socket.io.js文件的问题
按照socket.io官网的例子,写了服务端和客户端:
【server:】
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);
});
});
【client:】
<!doctype html>
<html>
<head>
<title>Socket.io Test</title>
</head>
<body>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</html>
服务器可以跑起来,客户端运行老是报错,提示找不到socket.io.js文件,没有io.connect这个方法,求高人指教!!