关于nodejs express框架连接socket.io的基础使用
具体请看 socket.io官方网站
installing
$ npm install socket.io [–save]
Using with Express 3 and 4
Server(app.js)
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
socket.on('disconnect', function () {
io.emit('user disconnected',{data:'exited'});
});
});
Client(index.html)
<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', { data: 'hi hi hi' });
});
</script>
index.html 直接引用就可以了
ok, express框架的socket.io的最最最基本使用就到这里