是否支持websocket浏览器可以自己判断 为什么socket.io还要发ajax请求?
不管是firefox,还是chrome ,只要用socket.io连接,都会发送ajax去请求服务器,直接用websocket连接会有什么问题呢?
2 回复
不指定连接协议,socket.io会根据浏览器状况自己判断用ws还是ajax
app.js
var fs = require('fs');
var http = require('http');
var sio = require('socket.io');
var app = http.createServer(function (req,res){
var data = fs.readFileSync(__dirname + '/client.html');
res.writeHead(200);
res.end(data);
});
var io = sio(app);
app.listen(80);
client.html
<!DOCTYPE HTML>
<html lang="ru-RU">
<head>
<title></title>
<meta charset="UTF-8">
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script>
var sio = io.connect();
</script>
</body>
</html>