服务器用nodejs实现
server2.js
Javascript代码
var http= require(‘http‘), io= require(‘socket.io‘), express= require(‘express‘);
var app = express.createServer(), io = io.listen(app);
app.listen(80);
io.sockets.on(‘connection‘, function (socket) {
socket.emit(‘news‘, { hello: ‘world‘ });//监听,一旦客户端连接上,即发送数据,第一个参数‘new‘为数据名,第二个参数既为数据
socket.on(‘my other event‘, function (data) {//捕获客户端发送名为‘my other event‘的数据
console.log(data.my);
});