Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

engine.io怎么样启用polling?

$
0
0

代码是参照这上面的例子 https://www.npmjs.com/package/engine.io

server.js

var engine = require('engine.io');
var fs=require('fs');
var handle=function(req,resp){

var data=fs.readFileSync('./client.html');
resp.end(data);
console.log(req.url);
};
var http = require('http').createServer(handle).listen(3000);
var eioServer = engine.attach(http);
console.log(eioServer.transports);
eioServer.on('connection', function (socket) {
  socket.on('message', function(data){ console.log(data); socket.send('from server:'+data); });
  socket.on('close', function(){ });
});

client.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 
 <script src="//cdn.bootcss.com/engine.io-client/2.0.0/engine.io.min.js"></script>
</head>
	<body>
		<div id="output"></div>
	<script>
var socket= eio({transports :['polling', 'websocket']});
  socket.on('open', function(){
  alert('ok');
  socket.send('xxxx');
    socket.on('message', function(data){
 document.getElementById('output').innerHTML+=data;
 });
    socket.on('close', function(){});
  });
</script>

用firefox测试通信是正常的,firebug看网络面板,发送的也是http请求,但是换成ie8就没响应了 但是看网上介绍是支持polling的 Transports

polling: XHR / JSONP polling transport.
websocket: WebSocket transport.

Viewing all articles
Browse latest Browse all 14821

Trending Articles