每当我执行我的代码时,我都会在控制台收到消息说它已连接,然后什么都没有发生。
这是代码:
服务器端
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile('index.html');
});
io.sockets.on('connection', function (socket) {
console.log('m connected');
socket.emit('news', { hello: 'm joooon ' });
socket.on('my other event', function (data) {
console.log(data);
});
});
客户端
<!DOCTYPE html>
<html>
<head>
<script 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>
</head>
<body>
<div id="lo">magudi</div>
</body></html>